Posted on

Description

Submission

class Solution {
public:
    bool isNStraightHand(vector<int>& hand, int groupSize) {
        if(hand.size() % groupSize != 0) return false;
        map<int, int> Map;

        for(int x: hand) {
            ++Map[x];
        }

        while(!Map.empty()) {
            int start = Map.begin()->first;
            for(int i = start; i < start + groupSize; ++i) {
                auto it = Map.find(i);
                if(it == Map.end()) return false;
                --it->second;
                if(it->second == 0) Map.erase(it);
            }
        }
        
        return true;
    }
};

Leave a Reply

Your email address will not be published. Required fields are marked *