-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Expand file tree
/
Copy path0846-hand-of-straights.ts
More file actions
34 lines (29 loc) · 875 Bytes
/
0846-hand-of-straights.ts
File metadata and controls
34 lines (29 loc) · 875 Bytes
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
function isNStraightHand(hand: number[], groupSize: number): boolean {
if (hand.length % groupSize) {
return false;
}
let count = {};
for (let n in hand) {
if (!count.hasOwnProperty(hand[n])) {
count[hand[n]] = 0;
}
count[hand[n]]++;
}
let sortUniqHand = [...new Set(hand)].sort((a, b) => b - a);
while (sortUniqHand.length > 0) {
let first = sortUniqHand[sortUniqHand.length - 1];
for (let i = first; i < first + groupSize; i++) {
if (!count.hasOwnProperty(i)) {
return false;
}
count[i]--;
if (count[i] === 0) {
if (i !== sortUniqHand[sortUniqHand.length - 1]) {
return false;
}
sortUniqHand.pop();
}
}
}
return true;
}