forked from luliyucoordinate/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
1f27abf
commit 3abd16c
Showing
7 changed files
with
84 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters