Skip to content

Commit 24fbc69

Browse files
authored
feat: add solutions for lc No.2946~2948 (#5115)
1 parent ed503a6 commit 24fbc69

9 files changed

Lines changed: 304 additions & 8 deletions

File tree

solution/2900-2999/2946.Matrix Similarity After Cyclic Shifts/README.md

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,17 @@ tags:
7474

7575
<!-- solution:start -->
7676

77-
### 方法一
77+
### 方法一:模拟
78+
79+
我们遍历矩阵的每个元素,判断其在循环移位后的位置是否与原位置的元素相同。
80+
81+
对于奇数行,我们将元素向右移动 $k$ 个位置,因此元素 $(i, j)$ 在循环移位后的位置为 $(i, (j + k) \bmod n)$,其中 $n$ 是矩阵的列数。
82+
83+
对于偶数行,我们将元素向左移动 $k$ 个位置,因此元素 $(i, j)$ 在循环移位后的位置为 $(i, (j - k + n) \bmod n)$。
84+
85+
如果在遍历过程中发现有任何一个元素在循环移位后的位置与原位置的元素不同,则返回 $\text{false}$。如果遍历完成后所有元素都相同,则返回 $\text{true}$。
86+
87+
时间复杂度 $O(m \times n)$,其中 $m$ 和 $n$ 分别是矩阵的行数和列数。空间复杂度 $O(1)$。
7888

7989
<!-- tabs:start -->
8090

@@ -179,6 +189,30 @@ function areSimilar(mat: number[][], k: number): boolean {
179189
}
180190
```
181191

192+
#### Rust
193+
194+
```rust
195+
impl Solution {
196+
pub fn are_similar(mat: Vec<Vec<i32>>, k: i32) -> bool {
197+
let m = mat.len();
198+
let n = mat[0].len();
199+
let k = (k as usize) % n;
200+
201+
for i in 0..m {
202+
for j in 0..n {
203+
if i % 2 == 1 && mat[i][j] != mat[i][(j + k) % n] {
204+
return false;
205+
}
206+
if i % 2 == 0 && mat[i][j] != mat[i][(j + n - k) % n] {
207+
return false;
208+
}
209+
}
210+
}
211+
true
212+
}
213+
}
214+
```
215+
182216
<!-- tabs:end -->
183217

184218
<!-- solution:end -->

solution/2900-2999/2946.Matrix Similarity After Cyclic Shifts/README_EN.md

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,17 @@ tags:
9494

9595
<!-- solution:start -->
9696

97-
### Solution 1
97+
### Solution 1: Simulation
98+
99+
We iterate over each element of the matrix and check whether its position after the cyclic shift is the same as the element at the original position.
100+
101+
For odd-indexed rows, we shift elements to the right by $k$ positions, so element $(i, j)$ moves to position $(i, (j + k) \bmod n)$ after the cyclic shift, where $n$ is the number of columns.
102+
103+
For even-indexed rows, we shift elements to the left by $k$ positions, so element $(i, j)$ moves to position $(i, (j - k + n) \bmod n)$ after the cyclic shift.
104+
105+
If at any point during the traversal we find that an element's position after the cyclic shift differs from the original, we return $\text{false}$. If all elements remain the same after the full traversal, we return $\text{true}$.
106+
107+
The time complexity is $O(m \times n)$, where $m$ and $n$ are the number of rows and columns of the matrix, respectively. The space complexity is $O(1)$.
98108

99109
<!-- tabs:start -->
100110

@@ -199,6 +209,30 @@ function areSimilar(mat: number[][], k: number): boolean {
199209
}
200210
```
201211

212+
#### Rust
213+
214+
```rust
215+
impl Solution {
216+
pub fn are_similar(mat: Vec<Vec<i32>>, k: i32) -> bool {
217+
let m = mat.len();
218+
let n = mat[0].len();
219+
let k = (k as usize) % n;
220+
221+
for i in 0..m {
222+
for j in 0..n {
223+
if i % 2 == 1 && mat[i][j] != mat[i][(j + k) % n] {
224+
return false;
225+
}
226+
if i % 2 == 0 && mat[i][j] != mat[i][(j + n - k) % n] {
227+
return false;
228+
}
229+
}
230+
}
231+
true
232+
}
233+
}
234+
```
235+
202236
<!-- tabs:end -->
203237

204238
<!-- solution:end -->
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
impl Solution {
2+
pub fn are_similar(mat: Vec<Vec<i32>>, k: i32) -> bool {
3+
let m = mat.len();
4+
let n = mat[0].len();
5+
let k = (k as usize) % n;
6+
7+
for i in 0..m {
8+
for j in 0..n {
9+
if i % 2 == 1 && mat[i][j] != mat[i][(j + k) % n] {
10+
return false;
11+
}
12+
if i % 2 == 0 && mat[i][j] != mat[i][(j + n - k) % n] {
13+
return false;
14+
}
15+
}
16+
}
17+
true
18+
}
19+
}

solution/2900-2999/2947.Count Beautiful Substrings I/README.md

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,11 @@ tags:
9393

9494
<!-- solution:start -->
9595

96-
### 方法一
96+
### 方法一:枚举
97+
98+
我们在 $[0, n)$ 范围内枚举子字符串的起始位置 $i$,在 $[i, n)$ 范围内枚举子字符串的结束位置 $j$,统计子字符串 $s[i \dots j]$ 中元音字母和辅音字母的数量,判断其是否为美丽字符串,如果是,则将答案加 $1$。
99+
100+
时间复杂度 $O(n^2)$,其中 $n$ 是字符串的长度。空间复杂度 $O(1)$。
97101

98102
<!-- tabs:start -->
99103

@@ -216,6 +220,37 @@ function beautifulSubstrings(s: string, k: number): number {
216220
}
217221
```
218222

223+
#### Rust
224+
225+
```rust
226+
impl Solution {
227+
pub fn beautiful_substrings(s: String, k: i32) -> i32 {
228+
let n = s.len();
229+
let s = s.as_bytes();
230+
231+
let mut vs = [0; 26];
232+
for &c in b"aeiou" {
233+
vs[(c - b'a') as usize] = 1;
234+
}
235+
236+
let mut ans = 0;
237+
238+
for i in 0..n {
239+
let mut vowels = 0;
240+
for j in i..n {
241+
vowels += vs[(s[j] - b'a') as usize];
242+
let consonants = (j - i + 1) as i32 - vowels;
243+
if vowels == consonants && (vowels * consonants) % k == 0 {
244+
ans += 1;
245+
}
246+
}
247+
}
248+
249+
ans
250+
}
251+
}
252+
```
253+
219254
<!-- tabs:end -->
220255

221256
<!-- solution:end -->

solution/2900-2999/2947.Count Beautiful Substrings I/README_EN.md

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ tags:
5151
<strong>Explanation:</strong> There are 2 beautiful substrings in the given string.
5252
- Substring &quot;b<u>aeyh</u>&quot;, vowels = 2 ([&quot;a&quot;,e&quot;]), consonants = 2 ([&quot;y&quot;,&quot;h&quot;]).
5353
You can see that string &quot;aeyh&quot; is beautiful as vowels == consonants and vowels * consonants % k == 0.
54-
- Substring &quot;<u>baey</u>h&quot;, vowels = 2 ([&quot;a&quot;,e&quot;]), consonants = 2 ([&quot;b&quot;,&quot;y&quot;]).
54+
- Substring &quot;<u>baey</u>h&quot;, vowels = 2 ([&quot;a&quot;,e&quot;]), consonants = 2 ([&quot;b&quot;,&quot;y&quot;]).
5555
You can see that string &quot;baey&quot; is beautiful as vowels == consonants and vowels * consonants % k == 0.
5656
It can be shown that there are only 2 beautiful substrings in the given string.
5757
</pre>
@@ -62,7 +62,7 @@ It can be shown that there are only 2 beautiful substrings in the given string.
6262
<strong>Input:</strong> s = &quot;abba&quot;, k = 1
6363
<strong>Output:</strong> 3
6464
<strong>Explanation:</strong> There are 3 beautiful substrings in the given string.
65-
- Substring &quot;<u>ab</u>ba&quot;, vowels = 1 ([&quot;a&quot;]), consonants = 1 ([&quot;b&quot;]).
65+
- Substring &quot;<u>ab</u>ba&quot;, vowels = 1 ([&quot;a&quot;]), consonants = 1 ([&quot;b&quot;]).
6666
- Substring &quot;ab<u>ba</u>&quot;, vowels = 1 ([&quot;a&quot;]), consonants = 1 ([&quot;b&quot;]).
6767
- Substring &quot;<u>abba</u>&quot;, vowels = 2 ([&quot;a&quot;,&quot;a&quot;]), consonants = 2 ([&quot;b&quot;,&quot;b&quot;]).
6868
It can be shown that there are only 3 beautiful substrings in the given string.
@@ -91,7 +91,11 @@ It can be shown that there are only 3 beautiful substrings in the given string.
9191

9292
<!-- solution:start -->
9393

94-
### Solution 1
94+
### Solution 1: Enumeration
95+
96+
We enumerate the starting position $i$ of the substring in the range $[0, n)$, and the ending position $j$ in the range $[i, n)$, count the number of vowels and consonants in the substring $s[i \dots j]$, and check whether it is a beautiful substring. If so, we increment the answer by $1$.
97+
98+
The time complexity is $O(n^2)$, where $n$ is the length of the string. The space complexity is $O(1)$.
9599

96100
<!-- tabs:start -->
97101

@@ -214,6 +218,37 @@ function beautifulSubstrings(s: string, k: number): number {
214218
}
215219
```
216220

221+
#### Rust
222+
223+
```rust
224+
impl Solution {
225+
pub fn beautiful_substrings(s: String, k: i32) -> i32 {
226+
let n = s.len();
227+
let s = s.as_bytes();
228+
229+
let mut vs = [0; 26];
230+
for &c in b"aeiou" {
231+
vs[(c - b'a') as usize] = 1;
232+
}
233+
234+
let mut ans = 0;
235+
236+
for i in 0..n {
237+
let mut vowels = 0;
238+
for j in i..n {
239+
vowels += vs[(s[j] - b'a') as usize];
240+
let consonants = (j - i + 1) as i32 - vowels;
241+
if vowels == consonants && (vowels * consonants) % k == 0 {
242+
ans += 1;
243+
}
244+
}
245+
}
246+
247+
ans
248+
}
249+
}
250+
```
251+
217252
<!-- tabs:end -->
218253

219254
<!-- solution:end -->
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
impl Solution {
2+
pub fn beautiful_substrings(s: String, k: i32) -> i32 {
3+
let n = s.len();
4+
let s = s.as_bytes();
5+
6+
let mut vs = [0; 26];
7+
for &c in b"aeiou" {
8+
vs[(c - b'a') as usize] = 1;
9+
}
10+
11+
let mut ans = 0;
12+
13+
for i in 0..n {
14+
let mut vowels = 0;
15+
for j in i..n {
16+
vowels += vs[(s[j] - b'a') as usize];
17+
let consonants = (j - i + 1) as i32 - vowels;
18+
if vowels == consonants && (vowels * consonants) % k == 0 {
19+
ans += 1;
20+
}
21+
}
22+
}
23+
24+
ans
25+
}
26+
}

solution/2900-2999/2948.Make Lexicographically Smallest Array by Swapping Elements/README.md

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,15 @@ tags:
7878

7979
<!-- solution:start -->
8080

81-
### 方法一
81+
### 方法一:排序
82+
83+
根据题目描述,排序后的数组 $\textit{nums}$ 可以划分成若干个子数组,使得每个子数组中的相邻元素之差不超过 $\textit{limit}$。
84+
85+
那么,可以通过交换得到的字典序最小的数组就是将每个子数组中的元素排序后,依次填入原数组所在位置得到的数组。
86+
87+
我们首先将数组 $\textit{nums}$ 中的元素和它们的下标组成一个二元组数组,并按照元素的值进行排序。然后,我们遍历排序后的二元组数组,找到每个子数组的范围,并将子数组中的元素按照下标进行排序后,填入原数组所在位置得到最终的结果。
88+
89+
时间复杂度 $O(n \times \log n)$,空间复杂度 $O(n)$,其中 $n$ 是数组 $\textit{nums}$ 的长度。
8290

8391
<!-- tabs:start -->
8492

@@ -209,6 +217,40 @@ function lexicographicallySmallestArray(nums: number[], limit: number): number[]
209217
}
210218
```
211219

220+
#### Rust
221+
222+
```rust
223+
impl Solution {
224+
pub fn lexicographically_smallest_array(nums: Vec<i32>, limit: i32) -> Vec<i32> {
225+
let n = nums.len();
226+
let mut idx: Vec<usize> = (0..n).collect();
227+
228+
idx.sort_by_key(|&i| nums[i]);
229+
230+
let mut ans = vec![0; n];
231+
232+
let mut i = 0;
233+
while i < n {
234+
let mut j = i + 1;
235+
while j < n && nums[idx[j]] - nums[idx[j - 1]] <= limit {
236+
j += 1;
237+
}
238+
239+
let mut t = idx[i..j].to_vec();
240+
t.sort();
241+
242+
for k in i..j {
243+
ans[t[k - i]] = nums[idx[k]];
244+
}
245+
246+
i = j;
247+
}
248+
249+
ans
250+
}
251+
}
252+
```
253+
212254
<!-- tabs:end -->
213255

214256
<!-- solution:end -->

solution/2900-2999/2948.Make Lexicographically Smallest Array by Swapping Elements/README_EN.md

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,15 @@ We cannot obtain a lexicographically smaller array by applying any more operatio
7676

7777
<!-- solution:start -->
7878

79-
### Solution 1
79+
### Solution 1: Sorting
80+
81+
According to the problem description, the sorted array $\textit{nums}$ can be partitioned into several subarrays such that the difference between adjacent elements in each subarray does not exceed $\textit{limit}$.
82+
83+
The lexicographically smallest array obtainable through swapping is thus the one where the elements within each subarray are sorted and placed back into their original positions in order.
84+
85+
We first pair each element in $\textit{nums}$ with its index to form an array of tuples, then sort by element value. We then traverse the sorted tuple array, identify the range of each subarray, sort the elements within each subarray by their original indices, and fill them back into the corresponding positions to obtain the final result.
86+
87+
The time complexity is $O(n \times \log n)$ and the space complexity is $O(n)$, where $n$ is the length of the array $\textit{nums}$.
8088

8189
<!-- tabs:start -->
8290

@@ -207,6 +215,40 @@ function lexicographicallySmallestArray(nums: number[], limit: number): number[]
207215
}
208216
```
209217

218+
#### Rust
219+
220+
```rust
221+
impl Solution {
222+
pub fn lexicographically_smallest_array(nums: Vec<i32>, limit: i32) -> Vec<i32> {
223+
let n = nums.len();
224+
let mut idx: Vec<usize> = (0..n).collect();
225+
226+
idx.sort_by_key(|&i| nums[i]);
227+
228+
let mut ans = vec![0; n];
229+
230+
let mut i = 0;
231+
while i < n {
232+
let mut j = i + 1;
233+
while j < n && nums[idx[j]] - nums[idx[j - 1]] <= limit {
234+
j += 1;
235+
}
236+
237+
let mut t = idx[i..j].to_vec();
238+
t.sort();
239+
240+
for k in i..j {
241+
ans[t[k - i]] = nums[idx[k]];
242+
}
243+
244+
i = j;
245+
}
246+
247+
ans
248+
}
249+
}
250+
```
251+
210252
<!-- tabs:end -->
211253

212254
<!-- solution:end -->

0 commit comments

Comments
 (0)