[프로그래머스 Level 2] 멀리 뛰기
1분 읽기
풀이 1: 경로 기반 누적합
function solution(n) {
const modular = 1234567;
const table = new Array(n+1).fill(0);
table[0] = 1; // n이 0인 경우는 없다.
for(let i = 0; i <= n; i++){
if(table[i]){
table[i+1] = (table[i+1] + table[i]) % modular;
table[i+2] += (table[i+2] + table[i]) % modular;
}
}
return table[n];
}풀이 2: 피보나치의 수열 기반
function solution(n) {
const modular = 1234567;
const table = new Array(n);
table[0] = 1; // 1가지 경우의 수 (1)
table[1] = 2; // 2가지 경우의 수 (1, 1), (2)
for(let i = 2; i <= n; i++){
table[i] = (table[i-1] + table[i-2]) % modular;
}
return table[n-1];
}Review
제한 사항
- n은 1 이상, 2000 이하인 정수입니다.
타겟 넘버와 유사
DP8편 중 7번째
관련 글
4분 읽기
소수 구하기
소수 판별법과 소수를 구하는 알고리즘을 정리합니다.
1분 읽기
[프로그래머스 Level 2] 미로 탈출
프로그래머스 미로 탈출을 BFS로 풀이합니다.
1분 읽기
[프로그래머스 Level 2] 마법의 엘리베이터
프로그래머스 마법의 엘리베이터를 자리수 그리디로 풀이합니다.