π₯ Algorithm/Programmers
[νλ‘κ·Έλλ¨Έμ€ Lv.2] nμ§μ κ²μ(js)
Lennon
2022. 1. 22. 07:11
728x90
λ°μν
https://programmers.co.kr/learn/courses/30/lessons/17687?language=javascript
function solution(n, t, m, p){
let changeNumber = [];
let answer = '';
let idx = p-1;
for(let i=0; i<t*m; i++){
changeNumber.push(i.toString(n));
}
changeNumber = changeNumber.join("");
while(answer.length < t){
answer+=changeNumber[idx].toUpperCase();
idx+=m;
}
return answer;
}
changeNumber λ°°μ΄μ iκ°μ λν΄ μ§μλ³νμ ν κ²μ pushνλ€.
κ·Έ ν joinμΌλ‘ λ¬Έμμ΄λ‘ λ§λ€μ΄μ€λ€.
ex) 2μ§μλΌλ©΄? => 0,1,2,3,4 => ['0', '1', '10', '11', '100'] => '011011100'
// t*mμ μ΅μν μ μ λλ λμμΌ λ¬Έμ κ° μκ² λ€κ³ μ μνλ€.
κ·Έ ν μμ(p-1) λ° μ°Έκ°μ μ(m)μ λ§ν΄μΌνλ μ(t)λ₯Ό κ³ λ €ν΄ whileλ¬ΈμΌλ‘ answerμ λ΅μ μΆκ°ν΄μ£Όκ³ 리ν΄ν΄μ£Όλ©΄ λλ€.
728x90
λ°μν