LeetCode/solutions/846. Hand of Straights.md
2020-02-16 20:13:31 +08:00

36 lines
1.6 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# [846. Hand of Straights](https://leetcode.com/problems/hand-of-straights/)
# 思路
这道题说手里有扑克牌若干是否能将手里的牌都以顺子的形式出完每组顺子的牌数都为W。
由于顺子是等差为1的等差数列所以如果知道了最小元素也就知道了其他所有元素。所以我们可以使用贪婪的算法首先取出手中最小的牌再按照不断+1的规则组成第一组顺子再在剩下的牌中按照上一次一样的规则组成第二组顺子以此类推。由此可见我们需要一个快速获得最小值和快速删除元素的数据结构map和multiset满足我们的要求。
若使用map我们可以记录元素值到出现次数的隐射删除操作即将出现次数减1若使用multiset我们每次根据迭代器删除元素即可即`.erase(.find(val))`(不能根据元素值删除`.erase(val)`那样会将所有值相等的元素都删了代码中我们使用multiset。
时间复杂度O(nlogn)空间复杂度O(n)
# C++
```C++
class Solution {
public:
bool isNStraightHand(vector<int>& hand, int W) {
int n = hand.size(), groups = n / W;
if(n % W) return false;
multiset<int>cards;
for(int num: hand) cards.insert(num);
int pre;
while(groups--){
pre = *cards.begin();
cards.erase(cards.begin());
for(int j = 1; j < W; j++){
if(cards.find(++pre) == cards.end()) return false;
cards.erase(cards.find(pre));
}
}
return true;
}
};
```