Skip to content

Commit e13d211

Browse files
committed
Add solution #914
1 parent 4d91c08 commit e13d211

File tree

2 files changed

+25
-0
lines changed

2 files changed

+25
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,7 @@
238238
884|[Uncommon Words from Two Sentences](./0884-uncommon-words-from-two-sentences.js)|Easy|
239239
890|[Find and Replace Pattern](./0890-find-and-replace-pattern.js)|Medium|
240240
905|[Sort Array By Parity](./0905-sort-array-by-parity.js)|Easy|
241+
914|[X of a Kind in a Deck of Cards](./0914-x-of-a-kind-in-a-deck-of-cards.js)|Medium|
241242
916|[Word Subsets](./0916-word-subsets.js)|Medium|
242243
922|[Sort Array By Parity II](./0922-sort-array-by-parity-ii.js)|Easy|
243244
925|[Long Pressed Name](./0925-long-pressed-name.js)|Easy|
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/**
2+
* 914. X of a Kind in a Deck of Cards
3+
* https://leetcode.com/problems/x-of-a-kind-in-a-deck-of-cards/
4+
* Difficulty: Medium
5+
*
6+
* You are given an integer array deck where deck[i] represents the number written on the ith card.
7+
*
8+
* Partition the cards into one or more groups such that:
9+
* - Each group has exactly x cards where x > 1, and
10+
* - All the cards in one group have the same integer written on them.
11+
*
12+
* Return true if such partition is possible, or false otherwise.
13+
*/
14+
15+
/**
16+
* @param {number[]} deck
17+
* @return {boolean}
18+
*/
19+
var hasGroupsSizeX = function(deck) {
20+
const gcd = (a, b) => !b ? a : gcd(b, a % b);
21+
const map = new Map();
22+
deck.forEach(n => map.set(n, (map.get(n) || 0) + 1));
23+
return [...map.values()].reduce(gcd) > 1;
24+
};

0 commit comments

Comments
 (0)