Skip to content

Commit fdae211

Browse files
committed
[level 2] Title: 피보나치 수, Time: 16.42 ms, Memory: 91.9 MB -BaekjoonHub
1 parent 6a69e4f commit fdae211

File tree

2 files changed

+17
-14
lines changed

2 files changed

+17
-14
lines changed

프로그래머스/2/12945. 피보나치 수/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
### 성능 요약
66

7-
메모리: 95.7 MB, 시간: 2.87 ms
7+
메모리: 91.9 MB, 시간: 16.42 ms
88

99
### 구분
1010

@@ -16,7 +16,7 @@
1616

1717
### 제출 일자
1818

19-
2025년 06월 02일 21:37:34
19+
2025년 06월 02일 21:43:39
2020

2121
### 문제 설명
2222

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,18 @@
11
class Solution {
2-
static int DP[];
3-
2+
static int[] memo;
3+
44
public int solution(int n) {
5-
DP = new int[n+1];
6-
DP[0] = 0;
7-
DP[1] = 1;
8-
9-
for(int i=2; i<=n; i++){
10-
DP[i] = (DP[i-1] + DP[i-2]) % 1234567;
11-
}
12-
// return DP[n] % 1234567;
13-
return DP[n];
5+
memo = new int[n+1];
6+
return fib(n);
147
}
15-
}
8+
9+
private int fib(int n) {
10+
if (n == 0) return 0;
11+
if (n == 1) return 1;
12+
if (memo[n] != 0) return memo[n]; // 이미 계산된 값이면 바로 반환
13+
14+
// 계산 후 저장(메모이제이션)
15+
memo[n] = (fib(n-1) + fib(n-2)) % 1234567;
16+
return memo[n];
17+
}
18+
}

0 commit comments

Comments
 (0)