-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy path0846-hand-of-straights.js
43 lines (39 loc) · 1.01 KB
/
0846-hand-of-straights.js
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
31
32
33
34
35
36
37
38
39
40
41
42
43
/**
* 846. Hand of Straights
* https://leetcode.com/problems/hand-of-straights/
* Difficulty: Medium
*
* Alice has some number of cards and she wants to rearrange
* the cards into groups so that each group is of size groupSize,
* and consists of groupSize consecutive cards.
*
* Given an integer array hand where hand[i] is the value written
* on the ith card and an integer groupSize, return true if she
* can rearrange the cards, or false otherwise.
*/
/**
* @param {number[]} hand
* @param {number} groupSize
* @return {boolean}
*/
var isNStraightHand = function(hand, groupSize) {
if (hand.length % groupSize) {
return false;
}
const map = {};
const set = new Set(hand);
hand.forEach(x => map[x] ? map[x]++ : map[x] = 1);
let count = hand.length / groupSize;
while (count--) {
let min = Math.min(...set);
for (let i = min; i < min + groupSize; i++) {
if (!map[i]) {
return false;
}
if (!--map[i]) {
set.delete(i);
}
}
}
return true;
};