Skip to content

feat: update solutions to lc problems: No.914,915,918 #2972

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion solution/0900-0999/0913.Cat and Mouse/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,13 @@ tags:

<!-- solution:start -->

### Solution 1
### Solution 1: Greatest Common Divisor

First, we use an array or hash table `cnt` to count the occurrence of each number. Only when $X$ is a divisor of the greatest common divisor of all `cnt[i]`, can it satisfy the problem's requirement.

Therefore, we find the greatest common divisor $g$ of the occurrence of all numbers, and then check whether it is greater than or equal to $2$.

The time complexity is $O(n \times \log M)$, and the space complexity is $O(n + \log M)$. Where $n$ and $M$ are the length of the array `deck` and the maximum value in the array `deck`, respectively.

<!-- tabs:start -->

Expand Down
69 changes: 39 additions & 30 deletions solution/0900-0999/0914.X of a Kind in a Deck of Cards/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ tags:

因此,我们求出所有数字出现次数的最大公约数 $g$,然后判断其是否大于等于 $2$ 即可。

时间复杂度 $O(n\log C)$,其中 $n$ 是数组 `deck` 的长度,而 $C$ 是数组 `deck` 中的最大值。
时间复杂度 $O(n \times \log M)$,空间复杂度 $O(n + \log M)$。其中 $n$ 和 $M$ 分别是数组 `deck` 的长度和数组 `deck` 中的最大值。

<!-- tabs:start -->

Expand All @@ -78,24 +78,22 @@ tags:
```python
class Solution:
def hasGroupsSizeX(self, deck: List[int]) -> bool:
vals = Counter(deck).values()
return reduce(gcd, vals) >= 2
cnt = Counter(deck)
return reduce(gcd, cnt.values()) >= 2
```

#### Java

```java
class Solution {
public boolean hasGroupsSizeX(int[] deck) {
int[] cnt = new int[10000];
for (int v : deck) {
++cnt[v];
Map<Integer, Integer> cnt = new HashMap<>();
for (int x : deck) {
cnt.merge(x, 1, Integer::sum);
}
int g = -1;
for (int v : cnt) {
if (v > 0) {
g = g == -1 ? v : gcd(g, v);
}
int g = cnt.get(deck[0]);
for (int x : cnt.values()) {
g = gcd(g, x);
}
return g >= 2;
}
Expand All @@ -112,13 +110,13 @@ class Solution {
class Solution {
public:
bool hasGroupsSizeX(vector<int>& deck) {
int cnt[10000] = {0};
for (int& v : deck) ++cnt[v];
int g = -1;
for (int& v : cnt) {
if (v) {
g = g == -1 ? v : __gcd(g, v);
}
unordered_map<int, int> cnt;
for (int x : deck) {
++cnt[x];
}
int g = cnt[deck[0]];
for (auto& [_, x] : cnt) {
g = gcd(g, x);
}
return g >= 2;
}
Expand All @@ -129,19 +127,13 @@ public:

```go
func hasGroupsSizeX(deck []int) bool {
cnt := make([]int, 10000)
for _, v := range deck {
cnt[v]++
cnt := map[int]int{}
for _, x := range deck {
cnt[x]++
}
g := -1
for _, v := range cnt {
if v > 0 {
if g == -1 {
g = v
} else {
g = gcd(g, v)
}
}
g := cnt[deck[0]]
for _, x := range cnt {
g = gcd(g, x)
}
return g >= 2
}
Expand All @@ -154,6 +146,23 @@ func gcd(a, b int) int {
}
```

#### TypeScript

```ts
function hasGroupsSizeX(deck: number[]): boolean {
const cnt: Record<number, number> = {};
for (const x of deck) {
cnt[x] = (cnt[x] || 0) + 1;
}
const gcd = (a: number, b: number): number => (b === 0 ? a : gcd(b, a % b));
let g = cnt[deck[0]];
for (const [_, x] of Object.entries(cnt)) {
g = gcd(g, x);
}
return g >= 2;
}
```

<!-- tabs:end -->

<!-- solution:end -->
Expand Down
75 changes: 45 additions & 30 deletions solution/0900-0999/0914.X of a Kind in a Deck of Cards/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,13 @@ tags:

<!-- solution:start -->

### Solution 1
### Solution 1: Greatest Common Divisor

First, we use an array or hash table `cnt` to count the occurrence of each number. Only when $X$ is a divisor of the greatest common divisor of all `cnt[i]`, can it satisfy the problem's requirement.

Therefore, we find the greatest common divisor $g$ of the occurrence of all numbers, and then check whether it is greater than or equal to $2$.

The time complexity is $O(n \times \log M)$, and the space complexity is $O(n + \log M)$. Where $n$ and $M$ are the length of the array `deck` and the maximum value in the array `deck`, respectively.

<!-- tabs:start -->

Expand All @@ -71,24 +77,22 @@ tags:
```python
class Solution:
def hasGroupsSizeX(self, deck: List[int]) -> bool:
vals = Counter(deck).values()
return reduce(gcd, vals) >= 2
cnt = Counter(deck)
return reduce(gcd, cnt.values()) >= 2
```

#### Java

```java
class Solution {
public boolean hasGroupsSizeX(int[] deck) {
int[] cnt = new int[10000];
for (int v : deck) {
++cnt[v];
Map<Integer, Integer> cnt = new HashMap<>();
for (int x : deck) {
cnt.merge(x, 1, Integer::sum);
}
int g = -1;
for (int v : cnt) {
if (v > 0) {
g = g == -1 ? v : gcd(g, v);
}
int g = cnt.get(deck[0]);
for (int x : cnt.values()) {
g = gcd(g, x);
}
return g >= 2;
}
Expand All @@ -105,13 +109,13 @@ class Solution {
class Solution {
public:
bool hasGroupsSizeX(vector<int>& deck) {
int cnt[10000] = {0};
for (int& v : deck) ++cnt[v];
int g = -1;
for (int& v : cnt) {
if (v) {
g = g == -1 ? v : __gcd(g, v);
}
unordered_map<int, int> cnt;
for (int x : deck) {
++cnt[x];
}
int g = cnt[deck[0]];
for (auto& [_, x] : cnt) {
g = gcd(g, x);
}
return g >= 2;
}
Expand All @@ -122,19 +126,13 @@ public:

```go
func hasGroupsSizeX(deck []int) bool {
cnt := make([]int, 10000)
for _, v := range deck {
cnt[v]++
cnt := map[int]int{}
for _, x := range deck {
cnt[x]++
}
g := -1
for _, v := range cnt {
if v > 0 {
if g == -1 {
g = v
} else {
g = gcd(g, v)
}
}
g := cnt[deck[0]]
for _, x := range cnt {
g = gcd(g, x)
}
return g >= 2
}
Expand All @@ -147,6 +145,23 @@ func gcd(a, b int) int {
}
```

#### TypeScript

```ts
function hasGroupsSizeX(deck: number[]): boolean {
const cnt: Record<number, number> = {};
for (const x of deck) {
cnt[x] = (cnt[x] || 0) + 1;
}
const gcd = (a: number, b: number): number => (b === 0 ? a : gcd(b, a % b));
let g = cnt[deck[0]];
for (const [_, x] of Object.entries(cnt)) {
g = gcd(g, x);
}
return g >= 2;
}
```

<!-- tabs:end -->

<!-- solution:end -->
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
class Solution {
public:
bool hasGroupsSizeX(vector<int>& deck) {
int cnt[10000] = {0};
for (int& v : deck) ++cnt[v];
int g = -1;
for (int& v : cnt) {
if (v) {
g = g == -1 ? v : __gcd(g, v);
}
unordered_map<int, int> cnt;
for (int x : deck) {
++cnt[x];
}
int g = cnt[deck[0]];
for (auto& [_, x] : cnt) {
g = gcd(g, x);
}
return g >= 2;
}
Expand Down
18 changes: 6 additions & 12 deletions solution/0900-0999/0914.X of a Kind in a Deck of Cards/Solution.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,11 @@
func hasGroupsSizeX(deck []int) bool {
cnt := make([]int, 10000)
for _, v := range deck {
cnt[v]++
cnt := map[int]int{}
for _, x := range deck {
cnt[x]++
}
g := -1
for _, v := range cnt {
if v > 0 {
if g == -1 {
g = v
} else {
g = gcd(g, v)
}
}
g := cnt[deck[0]]
for _, x := range cnt {
g = gcd(g, x)
}
return g >= 2
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
class Solution {
public boolean hasGroupsSizeX(int[] deck) {
int[] cnt = new int[10000];
for (int v : deck) {
++cnt[v];
Map<Integer, Integer> cnt = new HashMap<>();
for (int x : deck) {
cnt.merge(x, 1, Integer::sum);
}
int g = -1;
for (int v : cnt) {
if (v > 0) {
g = g == -1 ? v : gcd(g, v);
}
int g = cnt.get(deck[0]);
for (int x : cnt.values()) {
g = gcd(g, x);
}
return g >= 2;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
class Solution:
def hasGroupsSizeX(self, deck: List[int]) -> bool:
vals = Counter(deck).values()
return reduce(gcd, vals) >= 2
cnt = Counter(deck)
return reduce(gcd, cnt.values()) >= 2
12 changes: 12 additions & 0 deletions solution/0900-0999/0914.X of a Kind in a Deck of Cards/Solution.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
function hasGroupsSizeX(deck: number[]): boolean {
const cnt: Record<number, number> = {};
for (const x of deck) {
cnt[x] = (cnt[x] || 0) + 1;
}
const gcd = (a: number, b: number): number => (b === 0 ? a : gcd(b, a % b));
let g = cnt[deck[0]];
for (const [_, x] of Object.entries(cnt)) {
g = gcd(g, x);
}
return g >= 2;
}
Original file line number Diff line number Diff line change
Expand Up @@ -102,14 +102,13 @@ class Solution {
mi[i] = Math.min(nums[i], mi[i + 1]);
}
int mx = 0;
for (int i = 1; i <= n; ++i) {
for (int i = 1;; ++i) {
int v = nums[i - 1];
mx = Math.max(mx, v);
if (mx <= mi[i]) {
return i;
}
}
return 0;
}
}
```
Expand All @@ -122,14 +121,17 @@ public:
int partitionDisjoint(vector<int>& nums) {
int n = nums.size();
vector<int> mi(n + 1, INT_MAX);
for (int i = n - 1; ~i; --i) mi[i] = min(nums[i], mi[i + 1]);
for (int i = n - 1; ~i; --i) {
mi[i] = min(nums[i], mi[i + 1]);
}
int mx = 0;
for (int i = 1; i <= n; ++i) {
for (int i = 1;; ++i) {
int v = nums[i - 1];
mx = max(mx, v);
if (mx <= mi[i]) return i;
if (mx <= mi[i]) {
return i;
}
}
return 0;
}
};
```
Expand All @@ -145,14 +147,13 @@ func partitionDisjoint(nums []int) int {
mi[i] = min(nums[i], mi[i+1])
}
mx := 0
for i := 1; i <= n; i++ {
for i := 1; ; i++ {
v := nums[i-1]
mx = max(mx, v)
if mx <= mi[i] {
return i
}
}
return 0
}
```

Expand Down
Loading
Loading