Skip to content

Commit e3f683b

Browse files
authored
feat: update solutions to lc problems: No.914,915,918 (#2972)
1 parent 2f0b177 commit e3f683b

File tree

26 files changed

+237
-438
lines changed

26 files changed

+237
-438
lines changed

solution/0900-0999/0913.Cat and Mouse/README_EN.md

+7-1
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,13 @@ tags:
8080

8181
<!-- solution:start -->
8282

83-
### Solution 1
83+
### Solution 1: Greatest Common Divisor
84+
85+
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.
86+
87+
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$.
88+
89+
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.
8490

8591
<!-- tabs:start -->
8692

solution/0900-0999/0914.X of a Kind in a Deck of Cards/README.md

+39-30
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ tags:
6969

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

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

7474
<!-- tabs:start -->
7575

@@ -78,24 +78,22 @@ tags:
7878
```python
7979
class Solution:
8080
def hasGroupsSizeX(self, deck: List[int]) -> bool:
81-
vals = Counter(deck).values()
82-
return reduce(gcd, vals) >= 2
81+
cnt = Counter(deck)
82+
return reduce(gcd, cnt.values()) >= 2
8383
```
8484

8585
#### Java
8686

8787
```java
8888
class Solution {
8989
public boolean hasGroupsSizeX(int[] deck) {
90-
int[] cnt = new int[10000];
91-
for (int v : deck) {
92-
++cnt[v];
90+
Map<Integer, Integer> cnt = new HashMap<>();
91+
for (int x : deck) {
92+
cnt.merge(x, 1, Integer::sum);
9393
}
94-
int g = -1;
95-
for (int v : cnt) {
96-
if (v > 0) {
97-
g = g == -1 ? v : gcd(g, v);
98-
}
94+
int g = cnt.get(deck[0]);
95+
for (int x : cnt.values()) {
96+
g = gcd(g, x);
9997
}
10098
return g >= 2;
10199
}
@@ -112,13 +110,13 @@ class Solution {
112110
class Solution {
113111
public:
114112
bool hasGroupsSizeX(vector<int>& deck) {
115-
int cnt[10000] = {0};
116-
for (int& v : deck) ++cnt[v];
117-
int g = -1;
118-
for (int& v : cnt) {
119-
if (v) {
120-
g = g == -1 ? v : __gcd(g, v);
121-
}
113+
unordered_map<int, int> cnt;
114+
for (int x : deck) {
115+
++cnt[x];
116+
}
117+
int g = cnt[deck[0]];
118+
for (auto& [_, x] : cnt) {
119+
g = gcd(g, x);
122120
}
123121
return g >= 2;
124122
}
@@ -129,19 +127,13 @@ public:
129127
130128
```go
131129
func hasGroupsSizeX(deck []int) bool {
132-
cnt := make([]int, 10000)
133-
for _, v := range deck {
134-
cnt[v]++
130+
cnt := map[int]int{}
131+
for _, x := range deck {
132+
cnt[x]++
135133
}
136-
g := -1
137-
for _, v := range cnt {
138-
if v > 0 {
139-
if g == -1 {
140-
g = v
141-
} else {
142-
g = gcd(g, v)
143-
}
144-
}
134+
g := cnt[deck[0]]
135+
for _, x := range cnt {
136+
g = gcd(g, x)
145137
}
146138
return g >= 2
147139
}
@@ -154,6 +146,23 @@ func gcd(a, b int) int {
154146
}
155147
```
156148

149+
#### TypeScript
150+
151+
```ts
152+
function hasGroupsSizeX(deck: number[]): boolean {
153+
const cnt: Record<number, number> = {};
154+
for (const x of deck) {
155+
cnt[x] = (cnt[x] || 0) + 1;
156+
}
157+
const gcd = (a: number, b: number): number => (b === 0 ? a : gcd(b, a % b));
158+
let g = cnt[deck[0]];
159+
for (const [_, x] of Object.entries(cnt)) {
160+
g = gcd(g, x);
161+
}
162+
return g >= 2;
163+
}
164+
```
165+
157166
<!-- tabs:end -->
158167

159168
<!-- solution:end -->

solution/0900-0999/0914.X of a Kind in a Deck of Cards/README_EN.md

+45-30
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,13 @@ tags:
6262

6363
<!-- solution:start -->
6464

65-
### Solution 1
65+
### Solution 1: Greatest Common Divisor
66+
67+
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.
68+
69+
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$.
70+
71+
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.
6672

6773
<!-- tabs:start -->
6874

@@ -71,24 +77,22 @@ tags:
7177
```python
7278
class Solution:
7379
def hasGroupsSizeX(self, deck: List[int]) -> bool:
74-
vals = Counter(deck).values()
75-
return reduce(gcd, vals) >= 2
80+
cnt = Counter(deck)
81+
return reduce(gcd, cnt.values()) >= 2
7682
```
7783

7884
#### Java
7985

8086
```java
8187
class Solution {
8288
public boolean hasGroupsSizeX(int[] deck) {
83-
int[] cnt = new int[10000];
84-
for (int v : deck) {
85-
++cnt[v];
89+
Map<Integer, Integer> cnt = new HashMap<>();
90+
for (int x : deck) {
91+
cnt.merge(x, 1, Integer::sum);
8692
}
87-
int g = -1;
88-
for (int v : cnt) {
89-
if (v > 0) {
90-
g = g == -1 ? v : gcd(g, v);
91-
}
93+
int g = cnt.get(deck[0]);
94+
for (int x : cnt.values()) {
95+
g = gcd(g, x);
9296
}
9397
return g >= 2;
9498
}
@@ -105,13 +109,13 @@ class Solution {
105109
class Solution {
106110
public:
107111
bool hasGroupsSizeX(vector<int>& deck) {
108-
int cnt[10000] = {0};
109-
for (int& v : deck) ++cnt[v];
110-
int g = -1;
111-
for (int& v : cnt) {
112-
if (v) {
113-
g = g == -1 ? v : __gcd(g, v);
114-
}
112+
unordered_map<int, int> cnt;
113+
for (int x : deck) {
114+
++cnt[x];
115+
}
116+
int g = cnt[deck[0]];
117+
for (auto& [_, x] : cnt) {
118+
g = gcd(g, x);
115119
}
116120
return g >= 2;
117121
}
@@ -122,19 +126,13 @@ public:
122126
123127
```go
124128
func hasGroupsSizeX(deck []int) bool {
125-
cnt := make([]int, 10000)
126-
for _, v := range deck {
127-
cnt[v]++
129+
cnt := map[int]int{}
130+
for _, x := range deck {
131+
cnt[x]++
128132
}
129-
g := -1
130-
for _, v := range cnt {
131-
if v > 0 {
132-
if g == -1 {
133-
g = v
134-
} else {
135-
g = gcd(g, v)
136-
}
137-
}
133+
g := cnt[deck[0]]
134+
for _, x := range cnt {
135+
g = gcd(g, x)
138136
}
139137
return g >= 2
140138
}
@@ -147,6 +145,23 @@ func gcd(a, b int) int {
147145
}
148146
```
149147

148+
#### TypeScript
149+
150+
```ts
151+
function hasGroupsSizeX(deck: number[]): boolean {
152+
const cnt: Record<number, number> = {};
153+
for (const x of deck) {
154+
cnt[x] = (cnt[x] || 0) + 1;
155+
}
156+
const gcd = (a: number, b: number): number => (b === 0 ? a : gcd(b, a % b));
157+
let g = cnt[deck[0]];
158+
for (const [_, x] of Object.entries(cnt)) {
159+
g = gcd(g, x);
160+
}
161+
return g >= 2;
162+
}
163+
```
164+
150165
<!-- tabs:end -->
151166

152167
<!-- solution:end -->

solution/0900-0999/0914.X of a Kind in a Deck of Cards/Solution.cpp

+7-7
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
class Solution {
22
public:
33
bool hasGroupsSizeX(vector<int>& deck) {
4-
int cnt[10000] = {0};
5-
for (int& v : deck) ++cnt[v];
6-
int g = -1;
7-
for (int& v : cnt) {
8-
if (v) {
9-
g = g == -1 ? v : __gcd(g, v);
10-
}
4+
unordered_map<int, int> cnt;
5+
for (int x : deck) {
6+
++cnt[x];
7+
}
8+
int g = cnt[deck[0]];
9+
for (auto& [_, x] : cnt) {
10+
g = gcd(g, x);
1111
}
1212
return g >= 2;
1313
}

solution/0900-0999/0914.X of a Kind in a Deck of Cards/Solution.go

+6-12
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,11 @@
11
func hasGroupsSizeX(deck []int) bool {
2-
cnt := make([]int, 10000)
3-
for _, v := range deck {
4-
cnt[v]++
2+
cnt := map[int]int{}
3+
for _, x := range deck {
4+
cnt[x]++
55
}
6-
g := -1
7-
for _, v := range cnt {
8-
if v > 0 {
9-
if g == -1 {
10-
g = v
11-
} else {
12-
g = gcd(g, v)
13-
}
14-
}
6+
g := cnt[deck[0]]
7+
for _, x := range cnt {
8+
g = gcd(g, x)
159
}
1610
return g >= 2
1711
}

solution/0900-0999/0914.X of a Kind in a Deck of Cards/Solution.java

+6-8
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,12 @@
11
class Solution {
22
public boolean hasGroupsSizeX(int[] deck) {
3-
int[] cnt = new int[10000];
4-
for (int v : deck) {
5-
++cnt[v];
3+
Map<Integer, Integer> cnt = new HashMap<>();
4+
for (int x : deck) {
5+
cnt.merge(x, 1, Integer::sum);
66
}
7-
int g = -1;
8-
for (int v : cnt) {
9-
if (v > 0) {
10-
g = g == -1 ? v : gcd(g, v);
11-
}
7+
int g = cnt.get(deck[0]);
8+
for (int x : cnt.values()) {
9+
g = gcd(g, x);
1210
}
1311
return g >= 2;
1412
}
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
class Solution:
22
def hasGroupsSizeX(self, deck: List[int]) -> bool:
3-
vals = Counter(deck).values()
4-
return reduce(gcd, vals) >= 2
3+
cnt = Counter(deck)
4+
return reduce(gcd, cnt.values()) >= 2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
function hasGroupsSizeX(deck: number[]): boolean {
2+
const cnt: Record<number, number> = {};
3+
for (const x of deck) {
4+
cnt[x] = (cnt[x] || 0) + 1;
5+
}
6+
const gcd = (a: number, b: number): number => (b === 0 ? a : gcd(b, a % b));
7+
let g = cnt[deck[0]];
8+
for (const [_, x] of Object.entries(cnt)) {
9+
g = gcd(g, x);
10+
}
11+
return g >= 2;
12+
}

solution/0900-0999/0915.Partition Array into Disjoint Intervals/README.md

+9-8
Original file line numberDiff line numberDiff line change
@@ -102,14 +102,13 @@ class Solution {
102102
mi[i] = Math.min(nums[i], mi[i + 1]);
103103
}
104104
int mx = 0;
105-
for (int i = 1; i <= n; ++i) {
105+
for (int i = 1;; ++i) {
106106
int v = nums[i - 1];
107107
mx = Math.max(mx, v);
108108
if (mx <= mi[i]) {
109109
return i;
110110
}
111111
}
112-
return 0;
113112
}
114113
}
115114
```
@@ -122,14 +121,17 @@ public:
122121
int partitionDisjoint(vector<int>& nums) {
123122
int n = nums.size();
124123
vector<int> mi(n + 1, INT_MAX);
125-
for (int i = n - 1; ~i; --i) mi[i] = min(nums[i], mi[i + 1]);
124+
for (int i = n - 1; ~i; --i) {
125+
mi[i] = min(nums[i], mi[i + 1]);
126+
}
126127
int mx = 0;
127-
for (int i = 1; i <= n; ++i) {
128+
for (int i = 1;; ++i) {
128129
int v = nums[i - 1];
129130
mx = max(mx, v);
130-
if (mx <= mi[i]) return i;
131+
if (mx <= mi[i]) {
132+
return i;
133+
}
131134
}
132-
return 0;
133135
}
134136
};
135137
```
@@ -145,14 +147,13 @@ func partitionDisjoint(nums []int) int {
145147
mi[i] = min(nums[i], mi[i+1])
146148
}
147149
mx := 0
148-
for i := 1; i <= n; i++ {
150+
for i := 1; ; i++ {
149151
v := nums[i-1]
150152
mx = max(mx, v)
151153
if mx <= mi[i] {
152154
return i
153155
}
154156
}
155-
return 0
156157
}
157158
```
158159

0 commit comments

Comments
 (0)