GGym's Practice Notes

프로그래머스 Level3_ 2xn 타일링 (C++) 본문

Algorithm

프로그래머스 Level3_ 2xn 타일링 (C++)

GGym_ 2020. 10. 8. 23:26

규칙을 찾아보면 그냥 피보나치랑 똑같다.

DP로 착각할 수 있고 풀 수도 있겠지만 속도차이가 있을까...

 

#include <string>
#include <vector>

#include <iostream>

using namespace std;

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

    if (n == 1) answer = 1;
    else if (n == 2) answer = 2;
    else
    for(int i=2, j=1, k=2; i<n; i++){
        answer = (j + k)%1000000007;
        j = k;
        k = answer;
    }

    return answer;
}