-
Notifications
You must be signed in to change notification settings - Fork 42
/
solution.js
36 lines (34 loc) · 886 Bytes
/
solution.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
/**
* @param {number[]} hand
* @param {number} W
* @return {boolean}
*/
var isNStraightHand = function (hand, W) {
const set = [];
const map = {};
for (let i = 0; i < hand.length; i++) {
if (map[hand[i]] === undefined) {
set.push(hand[i]);
map[hand[i]] = 1;
} else {
map[hand[i]]++;
}
}
set.sort((a, b) => a - b);
for (let i = 0; i < set.length; i++) {
const curVal = set[i];
const curCount = map[curVal];
if (curCount === 0) {
continue;
}
// 向后查找看够不够用
for (let j = 1; j < W; j++) {
const nextCount = map[curVal + j];
if (nextCount === undefined || nextCount < curCount) {
return false;
}
map[curVal + j] -= curCount;
}
}
return true;
};