๋ฐ์ํ
https://programmers.co.kr/learn/courses/30/lessons/12945
์ฝ๋1(๋น์ฌ๊ท, DP)
class Solution {
public int solution(int n) {
int[] arr = new int[100001];
arr[1] = 1;
arr[2] = 1;
for(int i=3; i<=n; i++)
arr[i] = (arr[i-2] + arr[i-1]) % 1234567;
return arr[n];
}
}
ํ์ด
์ฝ๋2(์ฌ๊ท)
class Solution {
public static int fibo(int n){
if(n<=1)
return n;
else
return (fibo(n-2) + fibo(n-1)) % 1234567;
}
public int solution(int n) {
int answer = fibo(n);
return answer;
}
}
์ฌ๊ท๋ฅผ ์ด์ฉํ ๋ฐฉ์์ ํธ์ถ ํ๋ฒ๋น 2๋ฐฐ์ฉ(F(n) = F(n-2) + F(n-1)) ๋์ด๋๊ฒ ๋๊ณ , ์ด n๋จ๊ณ ์ด๋ฏ๋ก
BigO(2^N) ์ด๋ค.
์ฃผ์ด์ง n์ ๋ฒ์๋ 100000์ดํ์ธ๋ฐ, 2^100000์ ๋น์ฐํ ์๊ฐ์ด๊ณผ๊ฐ ๋ฐ์ํ๋ค.
2^30๋ง ํด๋ 10์ต,
๋น์ฌ๊ท(DP)๋ฅผ ์ด์ฉํ ๋ฐฉ์์ ์๊ฐ ๋ณต์ก๋๋ BigO(N) ์ด๋ค.
๋ฐ์ํ
'Algorithm' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
ํ๋ก๊ทธ๋๋จธ์ค[Java] - (Level2)์ฃผ์๊ฐ๊ฒฉ (0) | 2020.03.16 |
---|---|
ํ๋ก๊ทธ๋๋จธ์ค[Java] - (Level2)๊ฐ์ฅ ํฐ ์ ์ฌ๊ฐํ์ฐพ๊ธฐ(DP) (0) | 2020.03.16 |
ํ๋ก๊ทธ๋๋จธ์ค[Java] - (Level2)ํ (0) | 2020.03.16 |
[Codeforces] 1303A: Erasing Zeroes (0) | 2020.03.16 |
[๋ฐฑ์ค] 11945๋ฒ: ๋จ๊ฑฐ์ด ๋ถ์ด๋นต (0) | 2020.03.13 |
๋๊ธ