๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
Algorithm ๐Ÿง‘๐Ÿป‍๐Ÿ’ป/ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค(Programmers)

[c++] ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค - ํ”ผ๋ณด๋‚˜์น˜ ์ˆ˜( Level2)

by dkswnkk 2021. 11. 8.

๋ฌธ์ œ

 

์ฝ”๋”ฉํ…Œ์ŠคํŠธ ์—ฐ์Šต - ํ”ผ๋ณด๋‚˜์น˜ ์ˆ˜

ํ”ผ๋ณด๋‚˜์น˜ ์ˆ˜๋Š” F(0) = 0, F(1) = 1์ผ ๋•Œ, 1 ์ด์ƒ์˜ n์— ๋Œ€ํ•˜์—ฌ F(n) = F(n-1) + F(n-2) ๊ฐ€ ์ ์šฉ๋˜๋Š” ์ˆ˜ ์ž…๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ๋“ค์–ด F(2) = F(0) + F(1) = 0 + 1 = 1 F(3) = F(1) + F(2) = 1 + 1 = 2 F(4) = F(2) + F(3) = 1 + 2 = 3 F(5) = F(3) + F(4) =

programmers.co.kr

 

์ฝ”๋“œ

#include <string>
#include <vector>
#define MOD 1234567
using namespace std;

int dp[100001];

int solution(int n) {
    int answer = 0;

    dp[0]=0;
    dp[1]=1;

    for(int i=2; i<=n; i++){
        dp[i]=(dp[i-1]%MOD+dp[i-2]%MOD)%MOD;
    }
    answer=dp[n];

    return answer;
}

 

ํ’€์ด

bottom - up ๋ฐฉ์‹์˜ ๋‹จ์ˆœํ•œ dynamic-programming ๋ฌธ์ œ์˜€์Šต๋‹ˆ๋‹ค.

๋Œ“๊ธ€