Skip to content

Commit 4e278cd

Browse files
authored
feat: add solutions for lc No.3069,3070 (#5093)
1 parent 888a6ba commit 4e278cd

11 files changed

Lines changed: 237 additions & 84 deletions

File tree

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

Lines changed: 48 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -74,13 +74,13 @@ tags:
7474

7575
### 方法一:模拟
7676

77-
我们创建两个数组 `arr1``arr2`,分别存放 `nums` 中的元素,初始时 `arr1` 中只有 `nums[0]``arr2` 中只有 `nums[1]`
77+
我们创建两个数组 $\textit{arr1}$$\textit{arr2}$,分别存放数组 $\textit{nums}$ 中的元素,初始时 $\textit{arr1}$ 中只有 $\textit{nums[0]}$,$\textit{arr2}$ 中只有 $\textit{nums[1]}$
7878

79-
然后遍历 `nums` 下标从 $2$ 开始的元素,如果 `arr1` 的最后一个元素大于 `arr2` 的最后一个元素,就将当前元素追加到 `arr1`,否则追加到 `arr2`
79+
然后遍历 $\textit{nums}$ 下标从 $2$ 开始的元素,如果 $\textit{arr1}$ 的最后一个元素大于 $\textit{arr2}$ 的最后一个元素,就将当前元素追加到 $\textit{arr1}$,否则追加到 $\textit{arr2}$
8080

81-
最后将 `arr2` 中的元素追加到 `arr1` 中,返回 `arr1`
81+
最后将 $\textit{arr2}$ 中的元素追加到 $\textit{arr1}$ 中,返回 $\textit{arr1}$
8282

83-
时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 为数组 `nums` 的长度。
83+
时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 为数组 $\textit{nums}$ 的长度。
8484

8585
<!-- tabs:start -->
8686

@@ -181,6 +181,50 @@ function resultArray(nums: number[]): number[] {
181181
}
182182
```
183183

184+
#### Rust
185+
186+
```rust
187+
impl Solution {
188+
pub fn result_array(nums: Vec<i32>) -> Vec<i32> {
189+
let n = nums.len();
190+
let mut arr1 = vec![nums[0]];
191+
let mut arr2 = vec![nums[1]];
192+
for k in 2..n {
193+
if arr1[arr1.len() - 1] > arr2[arr2.len() - 1] {
194+
arr1.push(nums[k]);
195+
} else {
196+
arr2.push(nums[k]);
197+
}
198+
}
199+
arr1.extend(arr2);
200+
arr1
201+
}
202+
}
203+
```
204+
205+
#### C#
206+
207+
```cs
208+
public class Solution {
209+
public int[] ResultArray(int[] nums) {
210+
int n = nums.Length;
211+
var arr1 = new List<int> { nums[0] };
212+
var arr2 = new List<int> { nums[1] };
213+
214+
for (int k = 2; k < n; ++k) {
215+
if (arr1[arr1.Count - 1] > arr2[arr2.Count - 1]) {
216+
arr1.Add(nums[k]);
217+
} else {
218+
arr2.Add(nums[k]);
219+
}
220+
}
221+
222+
arr1.AddRange(arr2);
223+
return arr1.ToArray();
224+
}
225+
}
226+
```
227+
184228
<!-- tabs:end -->
185229

186230
<!-- solution:end -->

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

Lines changed: 48 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -72,13 +72,13 @@ Hence, the array result formed by concatenation is [5,3,4,8].
7272

7373
### Solution 1: Simulation
7474

75-
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]`.
75+
We create two arrays $\textit{arr1}$ and $\textit{arr2}$ to store the elements of $\textit{nums}$. Initially, $\textit{arr1}$ contains only $\textit{nums[0]}$, and $\textit{arr2}$ contains only $\textit{nums[1]}$.
7676

77-
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`.
77+
Then we iterate over the elements of $\textit{nums}$ starting from index $2$. If the last element of $\textit{arr1}$ is greater than the last element of $\textit{arr2}$, we append the current element to $\textit{arr1}$; otherwise, we append it to $\textit{arr2}$.
7878

79-
Finally, we append the elements in `arr2` to `arr1` and return `arr1`.
79+
Finally, we append the elements of $\textit{arr2}$ to $\textit{arr1}$ and return $\textit{arr1}$.
8080

81-
The time complexity is $O(n)$, and the space complexity is $O(n)$, where $n$ is the length of the array `nums`.
81+
The time complexity is $O(n)$, and the space complexity is $O(n)$, where $n$ is the length of the array $\textit{nums}$.
8282

8383
<!-- tabs:start -->
8484

@@ -179,6 +179,50 @@ function resultArray(nums: number[]): number[] {
179179
}
180180
```
181181

182+
#### Rust
183+
184+
```rust
185+
impl Solution {
186+
pub fn result_array(nums: Vec<i32>) -> Vec<i32> {
187+
let n = nums.len();
188+
let mut arr1 = vec![nums[0]];
189+
let mut arr2 = vec![nums[1]];
190+
for k in 2..n {
191+
if arr1[arr1.len() - 1] > arr2[arr2.len() - 1] {
192+
arr1.push(nums[k]);
193+
} else {
194+
arr2.push(nums[k]);
195+
}
196+
}
197+
arr1.extend(arr2);
198+
arr1
199+
}
200+
}
201+
```
202+
203+
#### C#
204+
205+
```cs
206+
public class Solution {
207+
public int[] ResultArray(int[] nums) {
208+
int n = nums.Length;
209+
var arr1 = new List<int> { nums[0] };
210+
var arr2 = new List<int> { nums[1] };
211+
212+
for (int k = 2; k < n; ++k) {
213+
if (arr1[arr1.Count - 1] > arr2[arr2.Count - 1]) {
214+
arr1.Add(nums[k]);
215+
} else {
216+
arr2.Add(nums[k]);
217+
}
218+
}
219+
220+
arr1.AddRange(arr2);
221+
return arr1.ToArray();
222+
}
223+
}
224+
```
225+
182226
<!-- tabs:end -->
183227

184228
<!-- solution:end -->
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
public class Solution {
2+
public int[] ResultArray(int[] nums) {
3+
int n = nums.Length;
4+
var arr1 = new List<int> { nums[0] };
5+
var arr2 = new List<int> { nums[1] };
6+
7+
for (int k = 2; k < n; ++k) {
8+
if (arr1[arr1.Count - 1] > arr2[arr2.Count - 1]) {
9+
arr1.Add(nums[k]);
10+
} else {
11+
arr2.Add(nums[k]);
12+
}
13+
}
14+
15+
arr1.AddRange(arr2);
16+
return arr1.ToArray();
17+
}
18+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
impl Solution {
2+
pub fn result_array(nums: Vec<i32>) -> Vec<i32> {
3+
let n = nums.len();
4+
let mut arr1 = vec![nums[0]];
5+
let mut arr2 = vec![nums[1]];
6+
for k in 2..n {
7+
if arr1[arr1.len() - 1] > arr2[arr2.len() - 1] {
8+
arr1.push(nums[k]);
9+
} else {
10+
arr2.push(nums[k]);
11+
}
12+
}
13+
arr1.extend(arr2);
14+
arr1
15+
}
16+
}

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

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,45 @@ function countSubmatrices(grid: number[][], k: number): number {
171171
}
172172
```
173173

174+
#### Rust
175+
176+
```rust
177+
impl Solution {
178+
pub fn count_submatrices(grid: Vec<Vec<i32>>, k: i32) -> i32 {
179+
let m = grid.len();
180+
let n = grid[0].len();
181+
let mut s = vec![vec![0; n + 1]; m + 1];
182+
let mut ans = 0;
183+
for i in 1..=m {
184+
for j in 1..=n {
185+
s[i][j] = s[i - 1][j] + s[i][j - 1] - s[i - 1][j - 1] + grid[i - 1][j - 1];
186+
if s[i][j] <= k { ans += 1; }
187+
}
188+
}
189+
ans
190+
}
191+
}
192+
```
193+
194+
#### C#
195+
196+
```cs
197+
public class Solution {
198+
public int CountSubmatrices(int[][] grid, int k) {
199+
int m = grid.Length, n = grid[0].Length;
200+
int[,] s = new int[m + 1, n + 1];
201+
int ans = 0;
202+
for (int i = 1; i <= m; ++i) {
203+
for (int j = 1; j <= n; ++j) {
204+
s[i, j] = s[i - 1, j] + s[i, j - 1] - s[i - 1, j - 1] + grid[i - 1][j - 1];
205+
if (s[i, j] <= k) ++ans;
206+
}
207+
}
208+
return ans;
209+
}
210+
}
211+
```
212+
174213
<!-- tabs:end -->
175214

176215
<!-- solution:end -->

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

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,45 @@ function countSubmatrices(grid: number[][], k: number): number {
169169
}
170170
```
171171

172+
#### Rust
173+
174+
```rust
175+
impl Solution {
176+
pub fn count_submatrices(grid: Vec<Vec<i32>>, k: i32) -> i32 {
177+
let m = grid.len();
178+
let n = grid[0].len();
179+
let mut s = vec![vec![0; n + 1]; m + 1];
180+
let mut ans = 0;
181+
for i in 1..=m {
182+
for j in 1..=n {
183+
s[i][j] = s[i - 1][j] + s[i][j - 1] - s[i - 1][j - 1] + grid[i - 1][j - 1];
184+
if s[i][j] <= k { ans += 1; }
185+
}
186+
}
187+
ans
188+
}
189+
}
190+
```
191+
192+
#### C#
193+
194+
```cs
195+
public class Solution {
196+
public int CountSubmatrices(int[][] grid, int k) {
197+
int m = grid.Length, n = grid[0].Length;
198+
int[,] s = new int[m + 1, n + 1];
199+
int ans = 0;
200+
for (int i = 1; i <= m; ++i) {
201+
for (int j = 1; j <= n; ++j) {
202+
s[i, j] = s[i - 1, j] + s[i, j - 1] - s[i - 1, j - 1] + grid[i - 1][j - 1];
203+
if (s[i, j] <= k) ++ans;
204+
}
205+
}
206+
return ans;
207+
}
208+
}
209+
```
210+
172211
<!-- tabs:end -->
173212

174213
<!-- solution:end -->
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
public 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) ++ans;
10+
}
11+
}
12+
return ans;
13+
}
14+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
impl Solution {
2+
pub fn count_submatrices(grid: Vec<Vec<i32>>, k: i32) -> i32 {
3+
let m = grid.len();
4+
let n = grid[0].len();
5+
let mut s = vec![vec![0; n + 1]; m + 1];
6+
let mut ans = 0;
7+
for i in 1..=m {
8+
for j in 1..=n {
9+
s[i][j] = s[i - 1][j] + s[i][j - 1] - s[i - 1][j - 1] + grid[i - 1][j - 1];
10+
if s[i][j] <= k { ans += 1; }
11+
}
12+
}
13+
ans
14+
}
15+
}

solution/3000-3099/3072.Distribute Elements Into Two Arrays II/README.md

Lines changed: 0 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -151,33 +151,6 @@ class Solution:
151151
return arr1 + arr2
152152
```
153153

154-
#### Python3
155-
156-
```python
157-
class Solution:
158-
def resultArray(self, nums: List[int]) -> List[int]:
159-
arr1 = [nums[0]]
160-
arr2 = [nums[1]]
161-
sl1 = SortedList(arr1)
162-
sl2 = SortedList(arr2)
163-
for x in nums[2:]:
164-
i = sl1.bisect_right(x)
165-
j = sl2.bisect_right(x)
166-
if len(sl1) - i > len(sl2) - j:
167-
arr1.append(x)
168-
sl1.add(x)
169-
elif len(sl1) - i < len(sl2) - j:
170-
arr2.append(x)
171-
sl2.add(x)
172-
elif len(sl1) <= len(sl2):
173-
arr1.append(x)
174-
sl1.add(x)
175-
else:
176-
arr2.append(x)
177-
sl2.add(x)
178-
return arr1 + arr2
179-
```
180-
181154
#### Java
182155

183156
```java

solution/3000-3099/3072.Distribute Elements Into Two Arrays II/README_EN.md

Lines changed: 0 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -149,33 +149,6 @@ class Solution:
149149
return arr1 + arr2
150150
```
151151

152-
#### Python3
153-
154-
```python
155-
class Solution:
156-
def resultArray(self, nums: List[int]) -> List[int]:
157-
arr1 = [nums[0]]
158-
arr2 = [nums[1]]
159-
sl1 = SortedList(arr1)
160-
sl2 = SortedList(arr2)
161-
for x in nums[2:]:
162-
i = sl1.bisect_right(x)
163-
j = sl2.bisect_right(x)
164-
if len(sl1) - i > len(sl2) - j:
165-
arr1.append(x)
166-
sl1.add(x)
167-
elif len(sl1) - i < len(sl2) - j:
168-
arr2.append(x)
169-
sl2.add(x)
170-
elif len(sl1) <= len(sl2):
171-
arr1.append(x)
172-
sl1.add(x)
173-
else:
174-
arr2.append(x)
175-
sl2.add(x)
176-
return arr1 + arr2
177-
```
178-
179152
#### Java
180153

181154
```java

0 commit comments

Comments
 (0)