Skip to content

Commit 3e04854

Browse files
authored
feat: add solutions to lc problems: No.2965~2968 (#2112)
* No.2965.Find Missing and Repeated Values * No.2966.Divide Array Into Arrays With Max Difference * No.2968.Apply Operations to Maximize Frequency Score
1 parent 37f4c1f commit 3e04854

File tree

21 files changed

+1063
-18
lines changed

21 files changed

+1063
-18
lines changed

solution/2900-2999/2965.Find Missing and Repeated Values/README.md

+104-3
Original file line numberDiff line numberDiff line change
@@ -46,34 +46,135 @@
4646

4747
<!-- 这里可写通用的实现逻辑 -->
4848

49+
**方法一:计数**
50+
51+
我们创建一个长度为 $n^2 + 1$ 的数组 $cnt$,统计矩阵中每个数字出现的次数。
52+
53+
接下来遍历 $i \in [1, n^2]$,如果 $cnt[i] = 2$,则 $i$ 是重复的数字,我们将答案的第一个元素设为 $i$;如果 $cnt[i] = 0$,则 $i$ 是缺失的数字,我们将答案的第二个元素设为 $i$。
54+
55+
时间复杂度 $O(n^2)$,空间复杂度 $O(n^2)$。其中 $n$ 是矩阵的边长。
56+
4957
<!-- tabs:start -->
5058

5159
### **Python3**
5260

5361
<!-- 这里可写当前语言的特殊实现逻辑 -->
5462

5563
```python
56-
64+
class Solution:
65+
def findMissingAndRepeatedValues(self, grid: List[List[int]]) -> List[int]:
66+
n = len(grid)
67+
cnt = [0] * (n * n + 1)
68+
for row in grid:
69+
for v in row:
70+
cnt[v] += 1
71+
ans = [0] * 2
72+
for i in range(1, n * n + 1):
73+
if cnt[i] == 2:
74+
ans[0] = i
75+
if cnt[i] == 0:
76+
ans[1] = i
77+
return ans
5778
```
5879

5980
### **Java**
6081

6182
<!-- 这里可写当前语言的特殊实现逻辑 -->
6283

6384
```java
64-
85+
class Solution {
86+
public int[] findMissingAndRepeatedValues(int[][] grid) {
87+
int n = grid.length;
88+
int[] cnt = new int[n * n + 1];
89+
int[] ans = new int[2];
90+
for (int[] row : grid) {
91+
for (int x : row) {
92+
if (++cnt[x] == 2) {
93+
ans[0] = x;
94+
}
95+
}
96+
}
97+
for (int x = 1;; ++x) {
98+
if (cnt[x] == 0) {
99+
ans[1] = x;
100+
return ans;
101+
}
102+
}
103+
}
104+
}
65105
```
66106

67107
### **C++**
68108

69109
```cpp
70-
110+
class Solution {
111+
public:
112+
vector<int> findMissingAndRepeatedValues(vector<vector<int>>& grid) {
113+
int n = grid.size();
114+
vector<int> cnt(n * n + 1);
115+
vector<int> ans(2);
116+
for (auto& row : grid) {
117+
for (int x : row) {
118+
if (++cnt[x] == 2) {
119+
ans[0] = x;
120+
}
121+
}
122+
}
123+
for (int x = 1;; ++x) {
124+
if (cnt[x] == 0) {
125+
ans[1] = x;
126+
return ans;
127+
}
128+
}
129+
}
130+
};
71131
```
72132
73133
### **Go**
74134
75135
```go
136+
func findMissingAndRepeatedValues(grid [][]int) []int {
137+
n := len(grid)
138+
ans := make([]int, 2)
139+
cnt := make([]int, n*n+1)
140+
for _, row := range grid {
141+
for _, x := range row {
142+
cnt[x]++
143+
if cnt[x] == 2 {
144+
ans[0] = x
145+
}
146+
}
147+
}
148+
for x := 1; ; x++ {
149+
if cnt[x] == 0 {
150+
ans[1] = x
151+
return ans
152+
}
153+
}
154+
}
155+
```
76156

157+
### **TypeScript**
158+
159+
```ts
160+
function findMissingAndRepeatedValues(grid: number[][]): number[] {
161+
const n = grid.length;
162+
const cnt: number[] = Array(n * n + 1).fill(0);
163+
const ans: number[] = Array(2).fill(0);
164+
for (const row of grid) {
165+
for (const x of row) {
166+
if (++cnt[x] === 2) {
167+
ans[0] = x;
168+
}
169+
}
170+
}
171+
for (let x = 1; ; ++x) {
172+
if (cnt[x] === 0) {
173+
ans[1] = x;
174+
return ans;
175+
}
176+
}
177+
}
77178
```
78179

79180
### **...**

solution/2900-2999/2965.Find Missing and Repeated Values/README_EN.md

+104-3
Original file line numberDiff line numberDiff line change
@@ -38,30 +38,131 @@
3838

3939
## Solutions
4040

41+
**Solution 1: Counting**
42+
43+
We create an array $cnt$ of length $n^2 + 1$ to count the frequency of each number in the matrix.
44+
45+
Next, we traverse $i \in [1, n^2]$. If $cnt[i] = 2$, then $i$ is the duplicated number, and we set the first element of the answer to $i$. If $cnt[i] = 0$, then $i$ is the missing number, and we set the second element of the answer to $i$.
46+
47+
The time complexity is $O(n^2)$, and the space complexity is $O(n^2)$. Here, $n$ is the side length of the matrix.
48+
4149
<!-- tabs:start -->
4250

4351
### **Python3**
4452

4553
```python
46-
54+
class Solution:
55+
def findMissingAndRepeatedValues(self, grid: List[List[int]]) -> List[int]:
56+
n = len(grid)
57+
cnt = [0] * (n * n + 1)
58+
for row in grid:
59+
for v in row:
60+
cnt[v] += 1
61+
ans = [0] * 2
62+
for i in range(1, n * n + 1):
63+
if cnt[i] == 2:
64+
ans[0] = i
65+
if cnt[i] == 0:
66+
ans[1] = i
67+
return ans
4768
```
4869

4970
### **Java**
5071

5172
```java
52-
73+
class Solution {
74+
public int[] findMissingAndRepeatedValues(int[][] grid) {
75+
int n = grid.length;
76+
int[] cnt = new int[n * n + 1];
77+
int[] ans = new int[2];
78+
for (int[] row : grid) {
79+
for (int x : row) {
80+
if (++cnt[x] == 2) {
81+
ans[0] = x;
82+
}
83+
}
84+
}
85+
for (int x = 1;; ++x) {
86+
if (cnt[x] == 0) {
87+
ans[1] = x;
88+
return ans;
89+
}
90+
}
91+
}
92+
}
5393
```
5494

5595
### **C++**
5696

5797
```cpp
58-
98+
class Solution {
99+
public:
100+
vector<int> findMissingAndRepeatedValues(vector<vector<int>>& grid) {
101+
int n = grid.size();
102+
vector<int> cnt(n * n + 1);
103+
vector<int> ans(2);
104+
for (auto& row : grid) {
105+
for (int x : row) {
106+
if (++cnt[x] == 2) {
107+
ans[0] = x;
108+
}
109+
}
110+
}
111+
for (int x = 1;; ++x) {
112+
if (cnt[x] == 0) {
113+
ans[1] = x;
114+
return ans;
115+
}
116+
}
117+
}
118+
};
59119
```
60120
61121
### **Go**
62122
63123
```go
124+
func findMissingAndRepeatedValues(grid [][]int) []int {
125+
n := len(grid)
126+
ans := make([]int, 2)
127+
cnt := make([]int, n*n+1)
128+
for _, row := range grid {
129+
for _, x := range row {
130+
cnt[x]++
131+
if cnt[x] == 2 {
132+
ans[0] = x
133+
}
134+
}
135+
}
136+
for x := 1; ; x++ {
137+
if cnt[x] == 0 {
138+
ans[1] = x
139+
return ans
140+
}
141+
}
142+
}
143+
```
64144

145+
### **TypeScript**
146+
147+
```ts
148+
function findMissingAndRepeatedValues(grid: number[][]): number[] {
149+
const n = grid.length;
150+
const cnt: number[] = Array(n * n + 1).fill(0);
151+
const ans: number[] = Array(2).fill(0);
152+
for (const row of grid) {
153+
for (const x of row) {
154+
if (++cnt[x] === 2) {
155+
ans[0] = x;
156+
}
157+
}
158+
}
159+
for (let x = 1; ; ++x) {
160+
if (cnt[x] === 0) {
161+
ans[1] = x;
162+
return ans;
163+
}
164+
}
165+
}
65166
```
66167

67168
### **...**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
class Solution {
2+
public:
3+
vector<int> findMissingAndRepeatedValues(vector<vector<int>>& grid) {
4+
int n = grid.size();
5+
vector<int> cnt(n * n + 1);
6+
vector<int> ans(2);
7+
for (auto& row : grid) {
8+
for (int x : row) {
9+
if (++cnt[x] == 2) {
10+
ans[0] = x;
11+
}
12+
}
13+
}
14+
for (int x = 1;; ++x) {
15+
if (cnt[x] == 0) {
16+
ans[1] = x;
17+
return ans;
18+
}
19+
}
20+
}
21+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
func findMissingAndRepeatedValues(grid [][]int) []int {
2+
n := len(grid)
3+
ans := make([]int, 2)
4+
cnt := make([]int, n*n+1)
5+
for _, row := range grid {
6+
for _, x := range row {
7+
cnt[x]++
8+
if cnt[x] == 2 {
9+
ans[0] = x
10+
}
11+
}
12+
}
13+
for x := 1; ; x++ {
14+
if cnt[x] == 0 {
15+
ans[1] = x
16+
return ans
17+
}
18+
}
19+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
class Solution {
2+
public int[] findMissingAndRepeatedValues(int[][] grid) {
3+
int n = grid.length;
4+
int[] cnt = new int[n * n + 1];
5+
int[] ans = new int[2];
6+
for (int[] row : grid) {
7+
for (int x : row) {
8+
if (++cnt[x] == 2) {
9+
ans[0] = x;
10+
}
11+
}
12+
}
13+
for (int x = 1;; ++x) {
14+
if (cnt[x] == 0) {
15+
ans[1] = x;
16+
return ans;
17+
}
18+
}
19+
}
20+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
class Solution:
2+
def findMissingAndRepeatedValues(self, grid: List[List[int]]) -> List[int]:
3+
n = len(grid)
4+
cnt = [0] * (n * n + 1)
5+
for row in grid:
6+
for v in row:
7+
cnt[v] += 1
8+
ans = [0] * 2
9+
for i in range(1, n * n + 1):
10+
if cnt[i] == 2:
11+
ans[0] = i
12+
if cnt[i] == 0:
13+
ans[1] = i
14+
return ans
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
function findMissingAndRepeatedValues(grid: number[][]): number[] {
2+
const n = grid.length;
3+
const cnt: number[] = Array(n * n + 1).fill(0);
4+
const ans: number[] = Array(2).fill(0);
5+
for (const row of grid) {
6+
for (const x of row) {
7+
if (++cnt[x] === 2) {
8+
ans[0] = x;
9+
}
10+
}
11+
}
12+
for (let x = 1; ; ++x) {
13+
if (cnt[x] === 0) {
14+
ans[1] = x;
15+
return ans;
16+
}
17+
}
18+
}

0 commit comments

Comments
 (0)