본문 바로가기
알고리즘

[프로그래머스] LV.3 정수 삼각형 (C++)

by kigo23 2023. 3. 22.
반응형
#include <string>
#include <vector>

using namespace std;

int solution(vector<vector<int>> dp) {
    int answer = 0;
    int tSize = dp.size();
    
    for(int i=1; i<dp.size(); i++){
        for(int j=1; j < dp[i].size()-1; j++){
            if(dp[i-1][j-1] > dp[i-1][j]){
                dp[i][j] += dp[i-1][j-1];
            }else{
                dp[i][j] += dp[i-1][j];
            }
        }
        dp[i][0] += dp[i-1][0];
        dp[i][dp[i].size()-1] += dp[i-1][dp[i-1].size()-1];
    }
    
    for(int i=0; i<tSize; i++){
        if(dp[tSize-1][i]>answer){
            answer = dp[tSize-1][i];
        }
    }
    
    return answer;
}