-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path846.hand-of-straights.js
More file actions
43 lines (34 loc) · 1.02 KB
/
Copy path846.hand-of-straights.js
File metadata and controls
43 lines (34 loc) · 1.02 KB
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
/**
* @param {number[]} cards
* @param {number} groupSize
* @return {boolean}
*/
var isNStraightHand = function (cards, groupSize) {
if (cards.length % groupSize !== 0) return false;
cards.sort((a, b) => a - b);
const countMap = new Map();
countCard();
let countRemoved = 0;
for (let card of cards) {
if (countRemoved === cards.length) break; // 所有牌都拿完的時候可以不用繼續跑完剩下的
if (countMap.get(card) === 0) continue;
// 以目前 card 為起始點按照 groupSize 檢查是否是連續的卡
for (let i = 0; i < groupSize; i++) {
let currentCard = card + i;
if (
countMap.get(currentCard) === undefined ||
countMap.get(currentCard) === 0
)
return false;
// 檢查一個就拿掉一張
countMap.set(currentCard, countMap.get(currentCard) - 1);
countRemoved++;
}
}
return true;
function countCard() {
for (let card of cards) {
countMap.set(card, (countMap.get(card) || 0) + 1);
}
}
};