forked from shijbian/LeetCode
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathhand-of-straights.cpp
More file actions
32 lines (29 loc) · 836 Bytes
/
hand-of-straights.cpp
File metadata and controls
32 lines (29 loc) · 836 Bytes
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
32
// Time: O(nlogn)
// Space: O(n)
class Solution {
public:
bool isNStraightHand(vector<int>& hand, int W) {
if (hand.size() % W) {
return false;
}
unordered_map<int, int> counts;
for (const auto& i : hand) {
++counts[i];
}
priority_queue<int, vector<int>, greater<int>> min_heap(hand.begin(), hand.end());
for (int i = 0; i < hand.size() / W; ++i) {
while (counts[min_heap.top()] == 0) {
min_heap.pop();
}
int start = min_heap.top(); min_heap.pop();
for (int j = 0; j < W; ++j) {
--counts[start];
if (counts[start] < 0) {
return false;
}
++start;
}
}
return true;
}
};