const solution = (n, t, m, p) => {
const limit = t * m;
let str = "";
let num = 0;
while (str.length < limit) {
str += num.toString(n);
num++;
}
let result = "";
for (let i = p - 1; i < limit; i += m) {
result += str[i];
}
return result.toUpperCase();
};
풀다가 복창 터질 뻔한 문제;;
처음에는 아래와 같이 재귀로 접근했는데 아무리 해봐도 테스트 케이스 6개를 통과하지 못했다.
대체 왜 반복문이면 되고 재귀면 안 되냐고
재귀가 콜 스택을 계속 잡아먹고 있어서(즉, 공간복잡도가 너무 커서) 그런가 싶긴 한데 정확한 원인 진단인지는 모르겠다.
const recur = (limit, dimension, num = 0, result = "") => {
if (result.length >= limit) {
return result;
}
result += num.toString(dimension);
return recur(limit, dimension, ++num, result);
};
const solution = (n, t, m, p) => {
const limit = t * m;
const str = recur(limit, n);
let result = "";
for (let i = p - 1; i < limit; i += m) {
result += str[i];
}
return result.toUpperCase();
};