반응형
문제 링크 : https://programmers.co.kr/learn/courses/30/lessons/87946
풀이
주어진 던전들을 순열로 돌며 직접 카운트해주면 된다.
순열을 사용하기 위해 정렬을 해줘야한다.
각 경우의 수마다 필요 피로도보다 현재 피로도가 크면 현재 피로도에서 소모 피로도를 빼주는 연산을 수행하고
카운트를 센 뒤 max를 통해 최댓값을 갱신한다.
코드
#include <bits/stdc++.h>
using namespace std;
int solution(int k, vector<vector<int>> dungeons) {
int answer = 0;
sort(dungeons.begin(), dungeons.end());
do {
int tmp = k, cnt = 0;
for(int i=0; i<dungeons.size(); i++){
if(tmp >= dungeons[i][0]){
cnt++;
tmp -= dungeons[i][1];
}
else break;
}
answer = max(answer, cnt);
} while(next_permutation(dungeons.begin(), dungeons.end()));
return answer;
}
반응형
'Programming Solve > 프로그래머스' 카테고리의 다른 글
프로그래머스 - 수식 최대화 / C++ (0) | 2022.04.22 |
---|---|
프로그래머스 - 오픈 채팅방 / C++ (0) | 2022.04.16 |
프로그래머스 - 행렬의 덧셈 / Swift (0) | 2022.04.01 |
프로그래머스 - K번째수 / Swift (0) | 2022.03.31 |
프로그래머스 - 파일명 정리 / C++ (0) | 2022.03.18 |