728x90
피보나치 & 메모이제이션(DP)
피보나치 수열로 생기는 타일 도형의 사각형 둘레를 계산하면 된다.
피보나치 수열의 범위가 80까지 이므로 메모이제이션을 활용하지 않으면 시간초과가 뜰것이라 생각해서
메모이제이션을 활용하여 문제를 풀었다.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <string> | |
#include <vector> | |
#include <iostream> | |
using namespace std; | |
long long memo[81]={0}; | |
long long pibo(int N) | |
{ | |
if(N==1 || N==2) | |
{ | |
return memo[N]; | |
} | |
else if(memo[N]!=0) | |
{ | |
return memo[N]; | |
} | |
else | |
{ | |
memo[N] = pibo(N-2) + pibo(N-1); | |
return memo[N]; | |
} | |
} | |
long long solution(int N) { | |
long long answer = 0; | |
memo[1]=1; | |
memo[2]=1; | |
answer = pibo(N); | |
answer = (memo[N-2] + memo[N-1]*2 + memo[N]) *2; | |
return answer; | |
} |
후기
다른사람 풀이보니까.. 진짜 똑똑한 사람들 많구나 싶다.
10줄이내로 해결이 되네...
'프로그래밍 > programmers&bj' 카테고리의 다른 글
[C++][알고리즘] 프로그래머스:: N으로 표현 (0) | 2020.04.15 |
---|---|
[C++][알고리즘] 백준 18808번 스티커 붙이기 (0) | 2020.03.26 |
[C++][SWEA] 1204. 최빈수 구하기 (0) | 2020.03.14 |
[C++][SWEA] 1206. View (0) | 2020.03.14 |
[C++][SWEA] 1247. 최적 경로 (0) | 2020.03.14 |