반응형
https://leetcode.com/problems/hand-of-straights
트리맵에 각 카드의 개수를 저장해줍니다.
트리의 첫번째 요소(가장 작은 값)를 꺼내고, groupSize만큼 연이은 카드가 남아있는지 검사해줍니다.
다 사용된 카드는 제거하고, 트리맵에서 제거해줍니다.
class Solution {
public:
bool isNStraightHand(vector<int>& hand, int groupSize) {
map<int, int> cardToCnt;
for(int card : hand) {
cardToCnt[card]++;
}
while(!cardToCnt.empty()) {
int card = cardToCnt.begin()->first;
for(int i=0; i<groupSize; i++) {
int& cnt = cardToCnt[card + i];
if(cnt == 0) return false;
if(--cnt == 0) cardToCnt.erase(card + i);
}
}
return true;
}
};
반응형
'기록' 카테고리의 다른 글
LeetCode 2192. All Ancestors of a Node in a Directed Acyclic Graph (0) | 2024.06.29 |
---|---|
LeetCode 1051. Height Checker (0) | 2024.06.23 |
LeetCode 945. Minimum Increment to Make Array Unique (0) | 2024.06.15 |
LeetCode 1122. Relative Sort Array (0) | 2024.06.12 |
한라산 (0) | 2021.12.24 |