Skip to content

Commit a83ea52

Browse files
committed
Add solution #846
1 parent 2fe08f2 commit a83ea52

File tree

2 files changed

+44
-0
lines changed

2 files changed

+44
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@
8989
824|[Goat Latin](./0824-goat-latin.js)|Easy|
9090
831|[Masking Personal Information](./0831-masking-personal-information.js)|Medium|
9191
844|[Backspace String Compare](./0844-backspace-string-compare.js)|Easy|
92+
846|[Hand of Straights](./0846-hand-of-straights.js)|Medium|
9293
867|[Transpose Matrix](./0867-transpose-matrix.js)|Easy|
9394
884|[Uncommon Words from Two Sentences](./0884-uncommon-words-from-two-sentences.js)|Easy|
9495
890|[Find and Replace Pattern](./0890-find-and-replace-pattern.js)|Medium|

solutions/0846-hand-of-straights.js

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/**
2+
* 846. Hand of Straights
3+
* https://leetcode.com/problems/hand-of-straights/
4+
* Difficulty: Medium
5+
*
6+
* Alice has some number of cards and she wants to rearrange
7+
* the cards into groups so that each group is of size groupSize,
8+
* and consists of groupSize consecutive cards.
9+
*
10+
* Given an integer array hand where hand[i] is the value written
11+
* on the ith card and an integer groupSize, return true if she
12+
* can rearrange the cards, or false otherwise.
13+
*/
14+
15+
/**
16+
* @param {number[]} hand
17+
* @param {number} groupSize
18+
* @return {boolean}
19+
*/
20+
var isNStraightHand = function(hand, groupSize) {
21+
if (hand.length % groupSize) {
22+
return false;
23+
}
24+
25+
const map = {};
26+
const set = new Set(hand);
27+
hand.forEach(x => map[x] ? map[x]++ : map[x] = 1);
28+
29+
let count = hand.length / groupSize;
30+
while (count--) {
31+
let min = Math.min(...set);
32+
for (let i = min; i < min + groupSize; i++) {
33+
if (!map[i]) {
34+
return false;
35+
}
36+
if (!--map[i]) {
37+
set.delete(i);
38+
}
39+
}
40+
}
41+
42+
return true;
43+
};

0 commit comments

Comments
 (0)