代码拉取完成,页面将自动刷新
// 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;
}
};
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。