Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 | 29 |
30 | 31 |
Tags
- Google Developer API
- spread 연산자
- Digital Ocean
- linux
- Unity Editor
- springboot
- Packet Network
- rpg server
- java
- SDK upgrade
- nodejs
- Google Refund
- Spring Boot
- server
- unity
- critical rendering path
- OverTheWire
- express
- react
- draganddrop
- Git
- docker
- mongoDB
- css framework
- Unity IAP
- --watch
- MySQL
- screencapture
- Camera Zoom
- Camera Movement
Archives
- Today
- Total
우당탕탕 개발일지
[프로그래머스] Level 3. 정수 삼각형 본문
https://school.programmers.co.kr/learn/courses/30/lessons/43105
프로그래머스
SW개발자를 위한 평가, 교육, 채용까지 Total Solution을 제공하는 개발자 성장을 위한 베이스캠프
programmers.co.kr
해결 방법
문제를 푸는 방법은 다양하게 있으나, 완전탐색으로 풀 경우 시간초과가 발생한다. 이때는 동적계획법을 사용해야한다. 또다른 triangle 인 tree변수를 만들어서, 그 자리에 올수있는 최대값만 저장하는 방식으로 문제를 푼다.
정수삼각형의 가운데요소는 다양한 경로로 도달할 수 있는데, 그중 가장 큰 값으로 갱신하면서 트리를 한줄씩 검사하는 방법이다.
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
int solution(vector<vector<int>> triangle) {
int answer =0;
vector<vector<int>> tree ;
tree.push_back(vector<int>(1,triangle[0][0])); //첫번째요소
for(int i=0; i<triangle.size()-1; i++){
tree.push_back(vector<int>(triangle[i+1].size(),0)); //다음열
for(int j=0; j<triangle[i].size(); j++){
tree[i+1][j] = max(tree[i+1][j],tree[i][j] + triangle[i+1][j]);
tree[i+1][j+1] = max(tree[i+1][j+1], tree[i][j] + triangle[i+1][j+1]);
}
}
int idx = triangle.size()-1;
sort(tree[idx].rbegin(), tree[idx].rend());
return tree[idx][0];
}
'Algorithm(c++) > Level 3' 카테고리의 다른 글
[프로그래머스] Level 3. 최고의 집합 (0) | 2025.02.25 |
---|---|
[프로그래머스] Level 3. 이중 우선순위 큐 (1) | 2025.02.23 |
[프로그래머스] Level 3. 외벽 점검 (1) | 2025.02.09 |
[프로그래머스] Level 3. 표현 가능한 이진트리 (2) | 2025.02.05 |
[프로그래머스] Level 3. 경주로 건설 (1) | 2025.02.05 |