Leetcode 876 Solution

This article provides solution to leetcode question 876 (hand-of-straights)

https://leetcode.com/problems/hand-of-straights

Solution

class Solution {
public:
    bool isNStraightHand(vector<int>& hand, int W) {
        map<int, int> a;

        for (int h: hand)
            a[h]++;

        while (!a.empty())
        {
            int s = a.begin()->first;

            for (int i = s; i < s + W; i++)
            {
                if (a.find(i) == a.end())
                    return false;

                a[i]--;

                if (a[i] == 0)
                    a.erase(i);
            }
        }

        return true;
    }
};