aboutsummaryrefslogtreecommitdiff
path: root/hand_of_straights.cpp
blob: b08c7fad3b2fdcca4d558370b06043f5732ffdcf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include "leetcode.h"

class Solution {
public:
  bool isNStraightHand(vector<int> &hand, int groupSize) {
    if (hand.size() % groupSize != 0)
      return false;
    map<int, int> m;
    for (auto &card : hand)
      m[card]++;
    for (int i = 0; i < groupSize; i++) {
      int curr = m.begin()->first;
      if (m[curr + i] == 0)
        return false;
      else if (--m[curr + i] < 1)
        m.erase(curr + i);
    }
    return true;
  }
};

int main() {
  Solution obj;
  vector<int> hand = {1, 2, 3, 6, 2, 3, 4, 7, 8};
  int groupSize = 3;
  if (obj.isNStraightHand(hand, groupSize))
    cout << "true";
  else
    cout << "false";
}