Skip to content

Commit

Permalink
add 914
Browse files Browse the repository at this point in the history
  • Loading branch information
luliyucoordinate committed Mar 27, 2020
1 parent 1f27abf commit 3abd16c
Show file tree
Hide file tree
Showing 7 changed files with 84 additions and 3 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,7 @@ LeetCode
|0901|[Online Stock Span](https://leetcode.com/problems/online-stock-span/) | c | [c++](./src/0901-Online-Stock-Span/0901.cpp) |[python](./src/0901-Online-Stock-Span/0901.py)||| |Medium|
|0902|[Numbers At Most N Given Digit Set](https://leetcode.com/problems/numbers-at-most-n-given-digit-set/) | c | [c++](./src/0902-Numbers-At-Most-N-Given-Digit-Set/0902.cpp) |[python](./src/0902-Numbers-At-Most-N-Given-Digit-Set/0902.py)||| |Hard|
|0907|[Sum of Subarray Minimums](https://leetcode.com/problems/sum-of-subarray-minimums/) | c | [c++](./src/0907-Sum-of-Subarray-Minimums/0907.cpp) |[python](./src/0907-Sum-of-Subarray-Minimums/0907.py)||| |Medium|
|0914|[X of a Kind in a Deck of Cards](https://leetcode.com/problems/x-of-a-kind-in-a-deck-of-cards/)|c|[c++](./src/0914-X-of-a-Kind-in-a-Deck-of-Cards/0914.cpp)|[python](./src/0914-X-of-a-Kind-in-a-Deck-of-Cards/0914.py)|[go](./src/0914-X-of-a-Kind-in-a-Deck-of-Cards/0914.go)|[js](./src/0914-X-of-a-Kind-in-a-Deck-of-Cards/0914.js)|[java](./src/0914-X-of-a-Kind-in-a-Deck-of-Cards/0914.java)|Easy|
|0917|[Reverse Only Letters](https://leetcode.com/problems/reverse-only-letters/) | c | [c++](./src/0917-Reverse-Only-Letters/0917.cpp) |[python](./src/0917-Reverse-Only-Letters/0917.py)||| |Easy|
|0918|[Maximum Sum Circular Subarray](https://leetcode.com/problems/maximum-sum-circular-subarray/) | c | [c++](./src/0918-Maximum-Sum-Circular-Subarray/0918.cpp) |[python](./src/0918-Maximum-Sum-Circular-Subarray/0918.py)||| |Medium|
|0919|[Complete Binary Tree Inserter](https://leetcode.com/problems/complete-binary-tree-inserter/) | c | [c++](./src/0919-Complete-Binary-Tree-Inserter/0919.cpp) |[python](./src/0919-Complete-Binary-Tree-Inserter/0919.py)||| |Medium|
Expand Down
16 changes: 16 additions & 0 deletions src/0914-X-of-a-Kind-in-a-Deck-of-Cards/0914.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
class Solution {
public:
bool hasGroupsSizeX(vector<int>& deck) {
int cnt[10000] = {};
for (int d : deck) cnt[d]++;

int res = -1;
for (int i = 0; i < 10000; i++) {
if (cnt[i]) {
if (res == -1) res = cnt[i];
else res = gcd(res, cnt[i]);
}
}
return res >= 2;
}
};
25 changes: 25 additions & 0 deletions src/0914-X-of-a-Kind-in-a-Deck-of-Cards/0914.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
func hasGroupsSizeX(deck []int) bool {
cnt := make([]int, 10000)
for _, d := range deck {
cnt[d]++
}

res := -1
for i := 0; i < 10000; i++ {
if cnt[i] > 0 {
if res == -1 {
res = cnt[i]
} else {
res = gcd(res, cnt[i])
}
}
}
return res >= 2
}

func gcd(a, b int) int {
if b == 0 {
return a
}
return gcd(b, a % b)
}
19 changes: 19 additions & 0 deletions src/0914-X-of-a-Kind-in-a-Deck-of-Cards/0914.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
class Solution {
public boolean hasGroupsSizeX(int[] deck) {
int[] cnt = new int[10000];
for (int d : deck) cnt[d]++;

int res = -1;
for (int i = 0; i < 10000; i++) {
if (cnt[i] > 0) {
if (res == -1) res = cnt[i];
else res = gcd(res, cnt[i]);
}
}
return res >= 2;
}

private int gcd(int a, int b) {
return b == 0 ? a : gcd(b, a % b);
}
}
17 changes: 17 additions & 0 deletions src/0914-X-of-a-Kind-in-a-Deck-of-Cards/0914.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
var hasGroupsSizeX = function(deck) {
let gcd = function(a, b) {
return b == 0 ? a : gcd(b, a % b);
}

let cnt = new Array(10000).fill(0);
for (let d of deck) cnt[d]++;

let res = -1;
for (let i = 0; i < 10000; i++) {
if (cnt[i] > 0) {
if (res == -1) res = cnt[i];
else res = gcd(res, cnt[i]);
}
}
return res >= 2;
};
3 changes: 3 additions & 0 deletions src/0914-X-of-a-Kind-in-a-Deck-of-Cards/0914.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
class Solution:
def hasGroupsSizeX(self, deck: List[int]) -> bool:
return functools.reduce(math.gcd, collections.Counter(deck).values()) >= 2
6 changes: 3 additions & 3 deletions src/addProb.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
import os, bisect

# 题目名称
name = "Middle of the Linked List"
ID = 876
url = "https://leetcode.com/problems/middle-of-the-linked-list/"
name = "X of a Kind in a Deck of Cards"
ID = 914
url = "https://leetcode.com/problems/x-of-a-kind-in-a-deck-of-cards/"
difficult = "Easy"
prog = ['c', 'cpp', 'py', 'go', 'js', 'java']

Expand Down

0 comments on commit 3abd16c

Please sign in to comment.