프로그래머스 문제풀이/LEVEL 2

[C++] 프로그래머스 문제풀이 LEVEL 2 땅따먹기

코딩준우 2023. 7. 5. 12:23

 

 

https://school.programmers.co.kr/learn/courses/30/lessons/12913

 

프로그래머스

코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.

programmers.co.kr

 

 

 

#include <bits/stdc++.h>


int DP[100001][4];
int solution(std::vector<std::vector<int>> land)
{
    int answer = 0;
    DP[0][0] = land[0][0];
    DP[0][1] = land[0][1];
    DP[0][2] = land[0][2];
    DP[0][3] = land[0][3];

    for (int i = 0; i < land.size(); ++i){
        DP[i][0] = land[i][0] + std::max(DP[i - 1][1], std::max(DP[i - 1][2], DP[i - 1][3]));
        DP[i][1] = land[i][1] + std::max(DP[i - 1][0], std::max(DP[i - 1][2], DP[i - 1][3]));
        DP[i][2] = land[i][2] + std::max(DP[i - 1][0], std::max(DP[i - 1][1], DP[i - 1][3]));
        DP[i][3] = land[i][3] + std::max(DP[i - 1][0], std::max(DP[i - 1][1], DP[i - 1][2]));
    }
    int index = land.size() - 1;
    answer = *std::max_element(DP[index], DP[index] + 4);
    return answer;
}