문제

제한사항
- 삼각형의 높이는 1이상 500 이하입니다.
- 삼각형을 이루고 있는 숫자는 0이상 9,999 이하의 정수입니다.
입출력

풀이
dp[i][j]
| 7 | ||||
| 10 | 15 | |||
| 18 | 16 | 15 | ||
| 20 | 25 | 20 | 19 | |
| 24 | 30 | 27 | 26 | 24 |
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
const int MAX = 501;
int dp[MAX][MAX];
int solution(vector<vector<int>> triangle) {
int answer = 0;
//초기 지정.
dp[0][0] = triangle[0][0];
for (int i = 1; i < triangle.size(); i++)
{
for (int j = 0; j <= i; j++)
{
//1) 행에서 젤 왼쪽인 경우엔 바로 위에꺼
if (j == 0) dp[i][j] = dp[i - 1][j] + triangle[i][j];
//2) 행에서 젤 오른쪽인 경우엔 왼쪽 위에꺼
else if (j == i) dp[i][j] = dp[i - 1][j - 1] + triangle[i][j];
//나머지
else dp[i][j] = max(dp[i - 1][j - 1], dp[i - 1][j]) + triangle[i][j];
// dp 배열의 제일 마지막 줄 중 가장 큰 값
answer = max(answer, dp[i][j]);
}
}
return answer;
}
https://school.programmers.co.kr/learn/courses/30/lessons/43105?language=cpp
프로그래머스
SW개발자를 위한 평가, 교육, 채용까지 Total Solution을 제공하는 개발자 성장을 위한 베이스캠프
programmers.co.kr
'Windows > Algorithm' 카테고리의 다른 글
| [백준] 14500번 테트로미노 | C/C++ (0) | 2025.01.30 |
|---|---|
| [프로그래머스] N으로 표현 | C/C++ (1) | 2025.01.28 |
| [백준] 1011번 Fly me to the Alpha Centauri | C/C++ (1) | 2025.01.27 |
| [프로그래머스] 전화번호 목록 | C/C++ (0) | 2024.10.29 |
| [프로그래머스] 더 멥게 | C/C++ (0) | 2024.10.17 |