https://programmers.co.kr/learn/courses/30/lessons/12913#
코딩테스트 연습 - 땅따먹기
땅따먹기 게임을 하려고 합니다. 땅따먹기 게임의 땅(land)은 총 N행 4열로 이루어져 있고, 모든 칸에는 점수가 쓰여 있습니다. 1행부터 땅을 밟으며 한 행씩 내려올 때, 각 행의 4칸 중 한 칸만 밟
programmers.co.kr
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int solution(vector<vector<int> > land)
{
for (int i = 1; i < land.size(); i++) {
land[i][0] += max(max(land[i-1][1],land[i-1][2] ), land[i - 1][3]);
land[i][1] += max(max(land[i - 1][0], land[i - 1][2]), land[i - 1][3]);
land[i][2] += max(max(land[i - 1][0], land[i - 1][1]), land[i - 1][3]);
land[i][3] += max(max(land[i - 1][0], land[i - 1][1]), land[i - 1][2]);
}
return *max_element(land[land.size() - 1].begin(), land[land.size() - 1].end());
}
DP는 코딩테스트에서 처음 써봐서 생각하는데 조금 걸렸다.
'전공공부 > 코딩테스트' 카테고리의 다른 글
(c++) 프로그래머스 "최댓값과 최솟값" (0) | 2022.04.18 |
---|---|
(c++) 프로그래머스 "쿼드압축 후 개수 세기" (0) | 2022.04.17 |
(c++) 프로그래머스 "올바른 괄호" (0) | 2022.04.15 |
(c++) 프로그래머스 "방문 길이" (0) | 2022.04.15 |
(c++) 프로그래머스 "2개 이하로 다른 비트" (0) | 2022.04.14 |