Skip to content

Commit 9fb8d56

Browse files
authoredMar 4, 2024
feat: add solutions to lc problems: No.3069,3070 (doocs#2406)
1 parent d1ee10f commit 9fb8d56

File tree

17 files changed

+490
-23
lines changed

17 files changed

+490
-23
lines changed
 

‎solution/0200-0299/0232.Implement Queue using Stacks/README.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ myQueue.empty(); // return false
7171

7272
### 方法一:双栈
7373

74-
使用两个栈,其中栈 `stk1`用于入队,另一个栈 `stk2` 用于出队。
74+
我们使用两个栈,其中栈 `stk1`用于入队,另一个栈 `stk2` 用于出队。
7575

7676
入队时,直接将元素入栈 `stk1`。时间复杂度 $O(1)$。
7777

@@ -282,7 +282,7 @@ class MyQueue {
282282

283283
peek(): number {
284284
this.move();
285-
return this.stk2[this.stk2.length - 1];
285+
return this.stk2.at(-1);
286286
}
287287

288288
empty(): boolean {
@@ -292,7 +292,7 @@ class MyQueue {
292292
move(): void {
293293
if (!this.stk2.length) {
294294
while (this.stk1.length) {
295-
this.stk2.push(this.stk1.pop());
295+
this.stk2.push(this.stk1.pop()!);
296296
}
297297
}
298298
}

‎solution/0200-0299/0232.Implement Queue using Stacks/README_EN.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,7 @@ class MyQueue {
260260

261261
peek(): number {
262262
this.move();
263-
return this.stk2[this.stk2.length - 1];
263+
return this.stk2.at(-1);
264264
}
265265

266266
empty(): boolean {
@@ -270,7 +270,7 @@ class MyQueue {
270270
move(): void {
271271
if (!this.stk2.length) {
272272
while (this.stk1.length) {
273-
this.stk2.push(this.stk1.pop());
273+
this.stk2.push(this.stk1.pop()!);
274274
}
275275
}
276276
}

‎solution/0200-0299/0232.Implement Queue using Stacks/Solution.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ class MyQueue {
1818

1919
peek(): number {
2020
this.move();
21-
return this.stk2[this.stk2.length - 1];
21+
return this.stk2.at(-1);
2222
}
2323

2424
empty(): boolean {
@@ -28,7 +28,7 @@ class MyQueue {
2828
move(): void {
2929
if (!this.stk2.length) {
3030
while (this.stk1.length) {
31-
this.stk2.push(this.stk1.pop());
31+
this.stk2.push(this.stk1.pop()!);
3232
}
3333
}
3434
}

‎solution/3000-3099/3069.Distribute Elements Into Two Arrays I/README.md

+83-4
Original file line numberDiff line numberDiff line change
@@ -57,24 +57,103 @@
5757

5858
## 解法
5959

60-
### 方法一
60+
### 方法一:模拟
61+
62+
我们创建两个数组 `arr1``arr2`,分别存放 `nums` 中的元素,初始时 `arr1` 中只有 `nums[0]``arr2` 中只有 `nums[1]`
63+
64+
然后遍历 `nums` 下标从 $2$ 开始的元素,如果 `arr1` 的最后一个元素大于 `arr2` 的最后一个元素,就将当前元素追加到 `arr1`,否则追加到 `arr2`
65+
66+
最后将 `arr2` 中的元素追加到 `arr1` 中,返回 `arr1`
67+
68+
时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 为数组 `nums` 的长度。
6169

6270
<!-- tabs:start -->
6371

6472
```python
65-
73+
class Solution:
74+
def resultArray(self, nums: List[int]) -> List[int]:
75+
arr1 = [nums[0]]
76+
arr2 = [nums[1]]
77+
for x in nums[2:]:
78+
if arr1[-1] > arr2[-1]:
79+
arr1.append(x)
80+
else:
81+
arr2.append(x)
82+
return arr1 + arr2
6683
```
6784

6885
```java
69-
86+
class Solution {
87+
public int[] resultArray(int[] nums) {
88+
int n = nums.length;
89+
int[] arr1 = new int[n];
90+
int[] arr2 = new int[n];
91+
arr1[0] = nums[0];
92+
arr2[0] = nums[1];
93+
int i = 0, j = 0;
94+
for (int k = 2; k < n; ++k) {
95+
if (arr1[i] > arr2[j]) {
96+
arr1[++i] = nums[k];
97+
} else {
98+
arr2[++j] = nums[k];
99+
}
100+
}
101+
for (int k = 0; k <= j; ++k) {
102+
arr1[++i] = arr2[k];
103+
}
104+
return arr1;
105+
}
106+
}
70107
```
71108

72109
```cpp
73-
110+
class Solution {
111+
public:
112+
vector<int> resultArray(vector<int>& nums) {
113+
int n = nums.size();
114+
vector<int> arr1 = {nums[0]};
115+
vector<int> arr2 = {nums[1]};
116+
for (int k = 2; k < n; ++k) {
117+
if (arr1.back() > arr2.back()) {
118+
arr1.push_back(nums[k]);
119+
} else {
120+
arr2.push_back(nums[k]);
121+
}
122+
}
123+
arr1.insert(arr1.end(), arr2.begin(), arr2.end());
124+
return arr1;
125+
}
126+
};
74127
```
75128
76129
```go
130+
func resultArray(nums []int) []int {
131+
arr1 := []int{nums[0]}
132+
arr2 := []int{nums[1]}
133+
for _, x := range nums[2:] {
134+
if arr1[len(arr1)-1] > arr2[len(arr2)-1] {
135+
arr1 = append(arr1, x)
136+
} else {
137+
arr2 = append(arr2, x)
138+
}
139+
}
140+
return append(arr1, arr2...)
141+
}
142+
```
77143

144+
```ts
145+
function resultArray(nums: number[]): number[] {
146+
const arr1: number[] = [nums[0]];
147+
const arr2: number[] = [nums[1]];
148+
for (const x of nums.slice(2)) {
149+
if (arr1.at(-1)! > arr2.at(-1)!) {
150+
arr1.push(x);
151+
} else {
152+
arr2.push(x);
153+
}
154+
}
155+
return arr1.concat(arr2);
156+
}
78157
```
79158

80159
<!-- tabs:end -->

‎solution/3000-3099/3069.Distribute Elements Into Two Arrays I/README_EN.md

+83-4
Original file line numberDiff line numberDiff line change
@@ -53,24 +53,103 @@ Hence, the array result formed by concatenation is [5,3,4,8].
5353

5454
## Solutions
5555

56-
### Solution 1
56+
### Solution 1: Simulation
57+
58+
We create two arrays `arr1` and `arr2`, which store the elements in `nums`. Initially, `arr1` only contains `nums[0]`, and `arr2` only contains `nums[1]`.
59+
60+
Then we traverse the elements of `nums` starting from index 2. If the last element of `arr1` is greater than the last element of `arr2`, we append the current element to `arr1`, otherwise we append it to `arr2`.
61+
62+
Finally, we append the elements in `arr2` to `arr1` and return `arr1`.
63+
64+
The time complexity is $O(n)$, and the space complexity is $O(n)$, where $n$ is the length of the array `nums`.
5765

5866
<!-- tabs:start -->
5967

6068
```python
61-
69+
class Solution:
70+
def resultArray(self, nums: List[int]) -> List[int]:
71+
arr1 = [nums[0]]
72+
arr2 = [nums[1]]
73+
for x in nums[2:]:
74+
if arr1[-1] > arr2[-1]:
75+
arr1.append(x)
76+
else:
77+
arr2.append(x)
78+
return arr1 + arr2
6279
```
6380

6481
```java
65-
82+
class Solution {
83+
public int[] resultArray(int[] nums) {
84+
int n = nums.length;
85+
int[] arr1 = new int[n];
86+
int[] arr2 = new int[n];
87+
arr1[0] = nums[0];
88+
arr2[0] = nums[1];
89+
int i = 0, j = 0;
90+
for (int k = 2; k < n; ++k) {
91+
if (arr1[i] > arr2[j]) {
92+
arr1[++i] = nums[k];
93+
} else {
94+
arr2[++j] = nums[k];
95+
}
96+
}
97+
for (int k = 0; k <= j; ++k) {
98+
arr1[++i] = arr2[k];
99+
}
100+
return arr1;
101+
}
102+
}
66103
```
67104

68105
```cpp
69-
106+
class Solution {
107+
public:
108+
vector<int> resultArray(vector<int>& nums) {
109+
int n = nums.size();
110+
vector<int> arr1 = {nums[0]};
111+
vector<int> arr2 = {nums[1]};
112+
for (int k = 2; k < n; ++k) {
113+
if (arr1.back() > arr2.back()) {
114+
arr1.push_back(nums[k]);
115+
} else {
116+
arr2.push_back(nums[k]);
117+
}
118+
}
119+
arr1.insert(arr1.end(), arr2.begin(), arr2.end());
120+
return arr1;
121+
}
122+
};
70123
```
71124
72125
```go
126+
func resultArray(nums []int) []int {
127+
arr1 := []int{nums[0]}
128+
arr2 := []int{nums[1]}
129+
for _, x := range nums[2:] {
130+
if arr1[len(arr1)-1] > arr2[len(arr2)-1] {
131+
arr1 = append(arr1, x)
132+
} else {
133+
arr2 = append(arr2, x)
134+
}
135+
}
136+
return append(arr1, arr2...)
137+
}
138+
```
73139

140+
```ts
141+
function resultArray(nums: number[]): number[] {
142+
const arr1: number[] = [nums[0]];
143+
const arr2: number[] = [nums[1]];
144+
for (const x of nums.slice(2)) {
145+
if (arr1.at(-1)! > arr2.at(-1)!) {
146+
arr1.push(x);
147+
} else {
148+
arr2.push(x);
149+
}
150+
}
151+
return arr1.concat(arr2);
152+
}
74153
```
75154

76155
<!-- tabs:end -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
class Solution {
2+
public:
3+
vector<int> resultArray(vector<int>& nums) {
4+
int n = nums.size();
5+
vector<int> arr1 = {nums[0]};
6+
vector<int> arr2 = {nums[1]};
7+
for (int k = 2; k < n; ++k) {
8+
if (arr1.back() > arr2.back()) {
9+
arr1.push_back(nums[k]);
10+
} else {
11+
arr2.push_back(nums[k]);
12+
}
13+
}
14+
arr1.insert(arr1.end(), arr2.begin(), arr2.end());
15+
return arr1;
16+
}
17+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
func resultArray(nums []int) []int {
2+
arr1 := []int{nums[0]}
3+
arr2 := []int{nums[1]}
4+
for _, x := range nums[2:] {
5+
if arr1[len(arr1)-1] > arr2[len(arr2)-1] {
6+
arr1 = append(arr1, x)
7+
} else {
8+
arr2 = append(arr2, x)
9+
}
10+
}
11+
return append(arr1, arr2...)
12+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
class Solution {
2+
public int[] resultArray(int[] nums) {
3+
int n = nums.length;
4+
int[] arr1 = new int[n];
5+
int[] arr2 = new int[n];
6+
arr1[0] = nums[0];
7+
arr2[0] = nums[1];
8+
int i = 0, j = 0;
9+
for (int k = 2; k < n; ++k) {
10+
if (arr1[i] > arr2[j]) {
11+
arr1[++i] = nums[k];
12+
} else {
13+
arr2[++j] = nums[k];
14+
}
15+
}
16+
for (int k = 0; k <= j; ++k) {
17+
arr1[++i] = arr2[k];
18+
}
19+
return arr1;
20+
}
21+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
class Solution:
2+
def resultArray(self, nums: List[int]) -> List[int]:
3+
arr1 = [nums[0]]
4+
arr2 = [nums[1]]
5+
for x in nums[2:]:
6+
if arr1[-1] > arr2[-1]:
7+
arr1.append(x)
8+
else:
9+
arr2.append(x)
10+
return arr1 + arr2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
function resultArray(nums: number[]): number[] {
2+
const arr1: number[] = [nums[0]];
3+
const arr2: number[] = [nums[1]];
4+
for (const x of nums.slice(2)) {
5+
if (arr1.at(-1)! > arr2.at(-1)!) {
6+
arr1.push(x);
7+
} else {
8+
arr2.push(x);
9+
}
10+
}
11+
return arr1.concat(arr2);
12+
}

‎solution/3000-3099/3070.Count Submatrices with Top-Left Element and Sum Less Than k/README.md

+86-4
Original file line numberDiff line numberDiff line change
@@ -43,24 +43,106 @@
4343

4444
## 解法
4545

46-
### 方法一
46+
### 方法一:二维前缀和
47+
48+
题目实际上求的是二维矩阵有多少个和小于等于 $k$ 的前缀子矩阵。
49+
50+
二维前缀和的计算公式为:
51+
52+
$$
53+
s[i][j] = s[i-1][j] + s[i][j-1] - s[i-1][j-1] + x
54+
$$
55+
56+
时间复杂度 $O(m \times n)$,空间复杂度 $O(m \times n)$。其中 $m$ 和 $n$ 分别是矩阵的行数和列数。
4757

4858
<!-- tabs:start -->
4959

5060
```python
51-
61+
class Solution:
62+
def countSubmatrices(self, grid: List[List[int]], k: int) -> int:
63+
s = [[0] * (len(grid[0]) + 1) for _ in range(len(grid) + 1)]
64+
ans = 0
65+
for i, row in enumerate(grid, 1):
66+
for j, x in enumerate(row, 1):
67+
s[i][j] = s[i - 1][j] + s[i][j - 1] - s[i - 1][j - 1] + x
68+
ans += s[i][j] <= k
69+
return ans
5270
```
5371

5472
```java
55-
73+
class Solution {
74+
public int countSubmatrices(int[][] grid, int k) {
75+
int m = grid.length, n = grid[0].length;
76+
int[][] s = new int[m + 1][n + 1];
77+
int ans = 0;
78+
for (int i = 1; i <= m; ++i) {
79+
for (int j = 1; j <= n; ++j) {
80+
s[i][j] = s[i - 1][j] + s[i][j - 1] - s[i - 1][j - 1] + grid[i - 1][j - 1];
81+
if (s[i][j] <= k) {
82+
++ans;
83+
}
84+
}
85+
}
86+
return ans;
87+
}
88+
}
5689
```
5790

5891
```cpp
59-
92+
class Solution {
93+
public:
94+
int countSubmatrices(vector<vector<int>>& grid, int k) {
95+
int m = grid.size(), n = grid[0].size();
96+
int s[m + 1][n + 1];
97+
memset(s, 0, sizeof(s));
98+
int ans = 0;
99+
for (int i = 1; i <= m; ++i) {
100+
for (int j = 1; j <= n; ++j) {
101+
s[i][j] = s[i - 1][j] + s[i][j - 1] - s[i - 1][j - 1] + grid[i - 1][j - 1];
102+
if (s[i][j] <= k) {
103+
++ans;
104+
}
105+
}
106+
}
107+
return ans;
108+
}
109+
};
60110
```
61111
62112
```go
113+
func countSubmatrices(grid [][]int, k int) (ans int) {
114+
s := make([][]int, len(grid)+1)
115+
for i := range s {
116+
s[i] = make([]int, len(grid[0])+1)
117+
}
118+
for i, row := range grid {
119+
for j, x := range row {
120+
s[i+1][j+1] = s[i+1][j] + s[i][j+1] - s[i][j] + x
121+
if s[i+1][j+1] <= k {
122+
ans++
123+
}
124+
}
125+
}
126+
return
127+
}
128+
```
63129

130+
```ts
131+
function countSubmatrices(grid: number[][], k: number): number {
132+
const m = grid.length;
133+
const n = grid[0].length;
134+
const s: number[][] = Array.from({ length: m + 1 }, () => Array(n + 1).fill(0));
135+
let ans: number = 0;
136+
for (let i = 1; i <= m; ++i) {
137+
for (let j = 1; j <= n; ++j) {
138+
s[i][j] = s[i - 1][j] + s[i][j - 1] - s[i - 1][j - 1] + grid[i - 1][j - 1];
139+
if (s[i][j] <= k) {
140+
++ans;
141+
}
142+
}
143+
}
144+
return ans;
145+
}
64146
```
65147

66148
<!-- tabs:end -->

‎solution/3000-3099/3070.Count Submatrices with Top-Left Element and Sum Less Than k/README_EN.md

+86-4
Original file line numberDiff line numberDiff line change
@@ -39,24 +39,106 @@
3939

4040
## Solutions
4141

42-
### Solution 1
42+
### Solution 1: Two-Dimensional Prefix Sum
43+
44+
The problem is actually asking for the number of prefix submatrices in a two-dimensional matrix whose sum is less than or equal to $k$.
45+
46+
The calculation formula for the two-dimensional prefix sum is:
47+
48+
$$
49+
s[i][j] = s[i-1][j] + s[i][j-1] - s[i-1][j-1] + x
50+
$$
51+
52+
The time complexity is $O(m \times n)$, and the space complexity is $O(m \times n)$, where $m$ and $n$ are the number of rows and columns of the matrix, respectively.
4353

4454
<!-- tabs:start -->
4555

4656
```python
47-
57+
class Solution:
58+
def countSubmatrices(self, grid: List[List[int]], k: int) -> int:
59+
s = [[0] * (len(grid[0]) + 1) for _ in range(len(grid) + 1)]
60+
ans = 0
61+
for i, row in enumerate(grid, 1):
62+
for j, x in enumerate(row, 1):
63+
s[i][j] = s[i - 1][j] + s[i][j - 1] - s[i - 1][j - 1] + x
64+
ans += s[i][j] <= k
65+
return ans
4866
```
4967

5068
```java
51-
69+
class Solution {
70+
public int countSubmatrices(int[][] grid, int k) {
71+
int m = grid.length, n = grid[0].length;
72+
int[][] s = new int[m + 1][n + 1];
73+
int ans = 0;
74+
for (int i = 1; i <= m; ++i) {
75+
for (int j = 1; j <= n; ++j) {
76+
s[i][j] = s[i - 1][j] + s[i][j - 1] - s[i - 1][j - 1] + grid[i - 1][j - 1];
77+
if (s[i][j] <= k) {
78+
++ans;
79+
}
80+
}
81+
}
82+
return ans;
83+
}
84+
}
5285
```
5386

5487
```cpp
55-
88+
class Solution {
89+
public:
90+
int countSubmatrices(vector<vector<int>>& grid, int k) {
91+
int m = grid.size(), n = grid[0].size();
92+
int s[m + 1][n + 1];
93+
memset(s, 0, sizeof(s));
94+
int ans = 0;
95+
for (int i = 1; i <= m; ++i) {
96+
for (int j = 1; j <= n; ++j) {
97+
s[i][j] = s[i - 1][j] + s[i][j - 1] - s[i - 1][j - 1] + grid[i - 1][j - 1];
98+
if (s[i][j] <= k) {
99+
++ans;
100+
}
101+
}
102+
}
103+
return ans;
104+
}
105+
};
56106
```
57107
58108
```go
109+
func countSubmatrices(grid [][]int, k int) (ans int) {
110+
s := make([][]int, len(grid)+1)
111+
for i := range s {
112+
s[i] = make([]int, len(grid[0])+1)
113+
}
114+
for i, row := range grid {
115+
for j, x := range row {
116+
s[i+1][j+1] = s[i+1][j] + s[i][j+1] - s[i][j] + x
117+
if s[i+1][j+1] <= k {
118+
ans++
119+
}
120+
}
121+
}
122+
return
123+
}
124+
```
59125

126+
```ts
127+
function countSubmatrices(grid: number[][], k: number): number {
128+
const m = grid.length;
129+
const n = grid[0].length;
130+
const s: number[][] = Array.from({ length: m + 1 }, () => Array(n + 1).fill(0));
131+
let ans: number = 0;
132+
for (let i = 1; i <= m; ++i) {
133+
for (let j = 1; j <= n; ++j) {
134+
s[i][j] = s[i - 1][j] + s[i][j - 1] - s[i - 1][j - 1] + grid[i - 1][j - 1];
135+
if (s[i][j] <= k) {
136+
++ans;
137+
}
138+
}
139+
}
140+
return ans;
141+
}
60142
```
61143

62144
<!-- tabs:end -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
class Solution {
2+
public:
3+
int countSubmatrices(vector<vector<int>>& grid, int k) {
4+
int m = grid.size(), n = grid[0].size();
5+
int s[m + 1][n + 1];
6+
memset(s, 0, sizeof(s));
7+
int ans = 0;
8+
for (int i = 1; i <= m; ++i) {
9+
for (int j = 1; j <= n; ++j) {
10+
s[i][j] = s[i - 1][j] + s[i][j - 1] - s[i - 1][j - 1] + grid[i - 1][j - 1];
11+
if (s[i][j] <= k) {
12+
++ans;
13+
}
14+
}
15+
}
16+
return ans;
17+
}
18+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
func countSubmatrices(grid [][]int, k int) (ans int) {
2+
s := make([][]int, len(grid)+1)
3+
for i := range s {
4+
s[i] = make([]int, len(grid[0])+1)
5+
}
6+
for i, row := range grid {
7+
for j, x := range row {
8+
s[i+1][j+1] = s[i+1][j] + s[i][j+1] - s[i][j] + x
9+
if s[i+1][j+1] <= k {
10+
ans++
11+
}
12+
}
13+
}
14+
return
15+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
class Solution {
2+
public int countSubmatrices(int[][] grid, int k) {
3+
int m = grid.length, n = grid[0].length;
4+
int[][] s = new int[m + 1][n + 1];
5+
int ans = 0;
6+
for (int i = 1; i <= m; ++i) {
7+
for (int j = 1; j <= n; ++j) {
8+
s[i][j] = s[i - 1][j] + s[i][j - 1] - s[i - 1][j - 1] + grid[i - 1][j - 1];
9+
if (s[i][j] <= k) {
10+
++ans;
11+
}
12+
}
13+
}
14+
return ans;
15+
}
16+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
class Solution:
2+
def countSubmatrices(self, grid: List[List[int]], k: int) -> int:
3+
s = [[0] * (len(grid[0]) + 1) for _ in range(len(grid) + 1)]
4+
ans = 0
5+
for i, row in enumerate(grid, 1):
6+
for j, x in enumerate(row, 1):
7+
s[i][j] = s[i - 1][j] + s[i][j - 1] - s[i - 1][j - 1] + x
8+
ans += s[i][j] <= k
9+
return ans
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
function countSubmatrices(grid: number[][], k: number): number {
2+
const m = grid.length;
3+
const n = grid[0].length;
4+
const s: number[][] = Array.from({ length: m + 1 }, () => Array(n + 1).fill(0));
5+
let ans: number = 0;
6+
for (let i = 1; i <= m; ++i) {
7+
for (let j = 1; j <= n; ++j) {
8+
s[i][j] = s[i - 1][j] + s[i][j - 1] - s[i - 1][j - 1] + grid[i - 1][j - 1];
9+
if (s[i][j] <= k) {
10+
++ans;
11+
}
12+
}
13+
}
14+
return ans;
15+
}

0 commit comments

Comments
 (0)
Please sign in to comment.