๋ฌธ์ ๐ฝ
https://school.programmers.co.kr/learn/courses/30/lessons/12914
๋ค๋ฅธ์ฌ๋ ํ์ด ๐ฝ
function solution(n) {
var answer = 0;
var dp=[];
dp[1]=1;
dp[2]=2;
for(var i=3;i<=n;i++){
dp[i]=dp[i-1]+dp[i-2] %1234567;
}
answer=dp[n];
return answer%1234567;
}
๋ดํ์ด๐ฝ
1. ์คํจ โ => ( ์๊ฐ์ด๊ณผ : dfs๋ก ์ฌ๊ท ์ฐ๋ฉด ๋๋ฌด ์ค๋๊ฑธ๋ฆผ )
// nC0 ~ nCn ๊น์ง ํ๋ฉด ๋ ๋ฏ ํ๋ค
function solution(n) {
var answer = 0;
const dfs =(count) =>{
if(count>n){
return 0;
}else if(count===n){
++answer
return 0;
}
dfs(count+1);
dfs(count+2);
}
dfs(0)
return answer%1234567;
}
2. ์ฑ๊ณต โญ => ( ํผ๋ณด๋์น ์์ด์ด์ฉ )
function solution(n) {
if(n===1){
return 1
}
if(n===2){
return 2
}
let array=[0,1,2]
for(let i=3; i<=n; i++){
array[i]=(array[i-2]+array[i-1])%1234567
}
return array[n];
}
๋๋์ ๐ฝ
array[i] = (array[i-2]+array[i-1]) %1234567 ์ ๊ฐ๊ณผ
array[i] = array[i-2]%1234567+array[i-1]%1234567์ ๊ฐ๊ณผ ๊ฐ๋ค