코딩공작소

[프로그래머스]정수삼각형 본문

알고리즘/DP

[프로그래머스]정수삼각형

안잡아모찌 2019. 10. 26. 18:17

https://programmers.co.kr/learn/courses/30/lessons/43105

 

코딩테스트 연습 - 정수 삼각형 | 프로그래머스

[[7], [3, 8], [8, 1, 0], [2, 7, 4, 4], [4, 5, 2, 6, 5]] 30

programmers.co.kr

어렵지 않은 DP.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
 
int solution(vector<vector<int>> triangle) {
    int answer = 0;
    vector<vector<int>> DP = triangle;
    int h = triangle.size();
    for(int i=1;i<h;i++) DP[i][0+= DP[i-1][0];
    for(int i=1;i<h;i++) DP[i][i] += DP[i-1][i-1];
    for(int i=2;i<h;i++){
        for(int j=1;j<triangle[i].size()-1;j++){
            DP[i][j] = triangle[i][j] + max(DP[i-1][j-1],DP[i-1][j]);
        }
    }
    for(auto var : DP[h-1]){
        answer=max(var,answer);
    }
    return answer;
}
cs

'알고리즘 > DP' 카테고리의 다른 글

[백준]내리막길  (0) 2019.10.10
DP,메모이제이션  (0) 2019.10.10
[백준]점프  (0) 2019.10.09
[SWEA]수영장  (0) 2019.08.18
[백준]퇴사(두번째 풂)  (0) 2019.07.11