Skip to content

Commit 05f58af

Browse files
authored
feat: add solutions for lc No.3874 (#5094)
1 parent 4e278cd commit 05f58af

32 files changed

Lines changed: 871 additions & 20 deletions

File tree

solution/2000-2099/2045.Second Minimum Time to Reach Destination/README_EN.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ The red path shows the path to get the second minimum time.
6262
- Wait at 4 for 4 minutes, time elapsed=10
6363
- 4 -> 5: 3 minutes, time elapsed=13
6464
Hence the second minimum time is 13 minutes.
65-
</pre>
65+
</pre>
6666

6767
<p><strong class="example">Example 2:</strong></p>
6868
<img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/2000-2099/2045.Second%20Minimum%20Time%20to%20Reach%20Destination/images/eg2.png" style="width: 225px; height: 50px;" />

solution/3200-3299/3212.Count Submatrices With Equal Frequency of X and Y/README.md

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,39 @@ function numberOfSubmatrices(grid: string[][]): number {
221221
}
222222
```
223223

224+
#### Rust
225+
226+
```rust
227+
impl Solution {
228+
pub fn number_of_submatrices(grid: Vec<Vec<char>>) -> i32 {
229+
let m = grid.len();
230+
let n = grid[0].len();
231+
let mut s = vec![vec![vec![0i32; 2]; n + 1]; m + 1];
232+
let mut ans = 0;
233+
234+
for i in 1..=m {
235+
for j in 1..=n {
236+
s[i][j][0] = s[i - 1][j][0]
237+
+ s[i][j - 1][0]
238+
- s[i - 1][j - 1][0]
239+
+ if grid[i - 1][j - 1] == 'X' { 1 } else { 0 };
240+
241+
s[i][j][1] = s[i - 1][j][1]
242+
+ s[i][j - 1][1]
243+
- s[i - 1][j - 1][1]
244+
+ if grid[i - 1][j - 1] == 'Y' { 1 } else { 0 };
245+
246+
if s[i][j][0] > 0 && s[i][j][0] == s[i][j][1] {
247+
ans += 1;
248+
}
249+
}
250+
}
251+
252+
ans
253+
}
254+
}
255+
```
256+
224257
<!-- tabs:end -->
225258

226259
<!-- solution:end -->

solution/3200-3299/3212.Count Submatrices With Equal Frequency of X and Y/README_EN.md

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,39 @@ function numberOfSubmatrices(grid: string[][]): number {
219219
}
220220
```
221221

222+
#### Rust
223+
224+
```rust
225+
impl Solution {
226+
pub fn number_of_submatrices(grid: Vec<Vec<char>>) -> i32 {
227+
let m = grid.len();
228+
let n = grid[0].len();
229+
let mut s = vec![vec![vec![0i32; 2]; n + 1]; m + 1];
230+
let mut ans = 0;
231+
232+
for i in 1..=m {
233+
for j in 1..=n {
234+
s[i][j][0] = s[i - 1][j][0]
235+
+ s[i][j - 1][0]
236+
- s[i - 1][j - 1][0]
237+
+ if grid[i - 1][j - 1] == 'X' { 1 } else { 0 };
238+
239+
s[i][j][1] = s[i - 1][j][1]
240+
+ s[i][j - 1][1]
241+
- s[i - 1][j - 1][1]
242+
+ if grid[i - 1][j - 1] == 'Y' { 1 } else { 0 };
243+
244+
if s[i][j][0] > 0 && s[i][j][0] == s[i][j][1] {
245+
ans += 1;
246+
}
247+
}
248+
}
249+
250+
ans
251+
}
252+
}
253+
```
254+
222255
<!-- tabs:end -->
223256

224257
<!-- solution:end -->
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
impl Solution {
2+
pub fn number_of_submatrices(grid: Vec<Vec<char>>) -> i32 {
3+
let m = grid.len();
4+
let n = grid[0].len();
5+
let mut s = vec![vec![vec![0i32; 2]; n + 1]; m + 1];
6+
let mut ans = 0;
7+
8+
for i in 1..=m {
9+
for j in 1..=n {
10+
s[i][j][0] = s[i - 1][j][0]
11+
+ s[i][j - 1][0]
12+
- s[i - 1][j - 1][0]
13+
+ if grid[i - 1][j - 1] == 'X' { 1 } else { 0 };
14+
15+
s[i][j][1] = s[i - 1][j][1]
16+
+ s[i][j - 1][1]
17+
- s[i - 1][j - 1][1]
18+
+ if grid[i - 1][j - 1] == 'Y' { 1 } else { 0 };
19+
20+
if s[i][j][0] > 0 && s[i][j][0] == s[i][j][1] {
21+
ans += 1;
22+
}
23+
}
24+
}
25+
26+
ans
27+
}
28+
}

solution/3800-3899/3865.Reverse K Subarrays/README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22
comments: true
33
difficulty: 中等
44
edit_url: https://github.com/doocs/leetcode/edit/main/solution/3800-3899/3865.Reverse%20K%20Subarrays/README.md
5+
tags:
6+
- 数组
7+
- 双指针
58
---
69

710
<!-- problem:start -->

solution/3800-3899/3865.Reverse K Subarrays/README_EN.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22
comments: true
33
difficulty: Medium
44
edit_url: https://github.com/doocs/leetcode/edit/main/solution/3800-3899/3865.Reverse%20K%20Subarrays/README_EN.md
5+
tags:
6+
- Array
7+
- Two Pointers
58
---
69

710
<!-- problem:start -->

solution/3800-3899/3866.First Unique Even Element/README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22
comments: true
33
difficulty: 简单
44
edit_url: https://github.com/doocs/leetcode/edit/main/solution/3800-3899/3866.First%20Unique%20Even%20Element/README.md
5+
tags:
6+
- 数组
7+
- 哈希表
8+
- 计数
59
---
610

711
<!-- problem:start -->

solution/3800-3899/3866.First Unique Even Element/README_EN.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22
comments: true
33
difficulty: Easy
44
edit_url: https://github.com/doocs/leetcode/edit/main/solution/3800-3899/3866.First%20Unique%20Even%20Element/README_EN.md
5+
tags:
6+
- Array
7+
- Hash Table
8+
- Counting
59
---
610

711
<!-- problem:start -->

solution/3800-3899/3867.Sum of GCD of Formed Pairs/README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,12 @@
22
comments: true
33
difficulty: 中等
44
edit_url: https://github.com/doocs/leetcode/edit/main/solution/3800-3899/3867.Sum%20of%20GCD%20of%20Formed%20Pairs/README.md
5+
tags:
6+
- 数组
7+
- 数学
8+
- 双指针
9+
- 数论
10+
- 模拟
511
---
612

713
<!-- problem:start -->

solution/3800-3899/3867.Sum of GCD of Formed Pairs/README_EN.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,12 @@
22
comments: true
33
difficulty: Medium
44
edit_url: https://github.com/doocs/leetcode/edit/main/solution/3800-3899/3867.Sum%20of%20GCD%20of%20Formed%20Pairs/README_EN.md
5+
tags:
6+
- Array
7+
- Math
8+
- Two Pointers
9+
- Number Theory
10+
- Simulation
511
---
612

713
<!-- problem:start -->

0 commit comments

Comments
 (0)