【贪心】【哈希表】个人练习-Leetcode-846. Hand of Straights

题目链接:https://leetcode.cn/problems/hand-of-straights/

题目大意:给出一数列,求是否能刚好将它们分成若干组,每组的元素数量为groupSize,并且元素连续。

思路:因为题目的限制很死,如果能够分那么分的结果一定是确定的。那么就可以贪心做。

先排序,并记录每种元素出现的次数,用哈希表cnt来记。然后从小到大遍历,每轮从【残存次数大于0的最小的数】开始(可以用一个vector来存这些数字,每个只存一次,起到一个set一样的效果),然后往后groupSize的数的残存次数都-1。如果中间任何一个地方出问题都会不合法,直接返回false

做完一下就通过了,但时间不是很理想。于是改了一下,每轮找【残存次数大于0的最小的数】的下标可以复用,直接从上一轮的继承就行了。结果果然快了不少。

完整代码

cpp 复制代码
class Solution {
public:
    bool isNStraightHand(vector<int>& hand, int groupSize) {
        int n = hand.size();
        if (n % groupSize)
            return false;
        sort(hand.begin(), hand.end());
        vector<int> nums;
        
        unordered_map<int, int> cnt;
        for (auto x : hand) {
            cnt[x]++;
            if (nums.size() == 0 || nums.back() != x)
                nums.emplace_back(x);
        }

        int lpn = n / groupSize;
        int idx = 0;
        for (int k = 0; k < lpn; k++) {
            while (cnt[nums[idx]] == 0)
                idx++;
            int num = nums[idx];
            if (cnt[num]) {
                cnt[num]--;
                for (int j = num+1; j < num + groupSize; j++) {
                    if (cnt[j] == 0)
                        return false;
                    else 
                        cnt[j]--;     
                }
            }
        }
        return true;
    }
};
相关推荐
前进的李工1 小时前
LeetCode hot100:094 二叉树的中序遍历:从递归到迭代的完整指南
python·算法·leetcode·链表·二叉树
兩尛3 小时前
215. 数组中的第K个最大元素
数据结构·算法·排序算法
952363 小时前
数据结构-堆
java·数据结构·学习·算法
吃着火锅x唱着歌3 小时前
LeetCode 面试题 16.24.数对和
算法·leetcode·职场和发展
不会编程的小寒3 小时前
数据结构 2.0
数据结构·算法
while(1){yan}3 小时前
MYSQL索引的底层数据结构
数据结构·数据库·mysql
Dream it possible!3 小时前
LeetCode 面试经典 150_二叉树层次遍历_二叉树的层平均值(82_637_C++_简单)
c++·leetcode·面试·二叉树
Wenhao.3 小时前
LeetCode Hot100 每日温度
数据结构·算法·leetcode·golang
吃着火锅x唱着歌3 小时前
LeetCode 1679.K和数对的最大数目
算法·leetcode·职场和发展
im_AMBER3 小时前
Leetcode 57
笔记·学习·算法·leetcode