Skip to content

Commit c038427

Browse files
committed
feat: add solutions to lc problems: No.3201,3202
* No.3201.Find the Maximum Length of Valid Subsequence I * No.3202.Find the Maximum Length of Valid Subsequence II
1 parent 97d7b82 commit c038427

File tree

24 files changed

+341
-79
lines changed

24 files changed

+341
-79
lines changed

solution/3200-3299/3201.Find the Maximum Length of Valid Subsequence I/README.md

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,47 @@ function maximumLength(nums: number[]): number {
201201
}
202202
```
203203

204+
#### Rust
205+
206+
```rust
207+
impl Solution {
208+
pub fn maximum_length(nums: Vec<i32>) -> i32 {
209+
let mut f = [[0; 2]; 2];
210+
let mut ans = 0;
211+
for x in nums {
212+
let x = (x % 2) as usize;
213+
for j in 0..2 {
214+
let y = ((j + 2 - x) % 2) as usize;
215+
f[x][y] = f[y][x] + 1;
216+
ans = ans.max(f[x][y]);
217+
}
218+
}
219+
ans
220+
}
221+
}
222+
```
223+
224+
#### C#
225+
226+
```cs
227+
public class Solution {
228+
public int MaximumLength(int[] nums) {
229+
int k = 2;
230+
int[,] f = new int[k, k];
231+
int ans = 0;
232+
foreach (int num in nums) {
233+
int x = num % k;
234+
for (int j = 0; j < k; ++j) {
235+
int y = (j - x + k) % k;
236+
f[x, y] = f[y, x] + 1;
237+
ans = Math.Max(ans, f[x, y]);
238+
}
239+
}
240+
return ans;
241+
}
242+
}
243+
```
244+
204245
<!-- tabs:end -->
205246

206247
<!-- solution:end -->

solution/3200-3299/3201.Find the Maximum Length of Valid Subsequence I/README_EN.md

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,47 @@ function maximumLength(nums: number[]): number {
199199
}
200200
```
201201

202+
#### Rust
203+
204+
```rust
205+
impl Solution {
206+
pub fn maximum_length(nums: Vec<i32>) -> i32 {
207+
let mut f = [[0; 2]; 2];
208+
let mut ans = 0;
209+
for x in nums {
210+
let x = (x % 2) as usize;
211+
for j in 0..2 {
212+
let y = ((j + 2 - x) % 2) as usize;
213+
f[x][y] = f[y][x] + 1;
214+
ans = ans.max(f[x][y]);
215+
}
216+
}
217+
ans
218+
}
219+
}
220+
```
221+
222+
#### C#
223+
224+
```cs
225+
public class Solution {
226+
public int MaximumLength(int[] nums) {
227+
int k = 2;
228+
int[,] f = new int[k, k];
229+
int ans = 0;
230+
foreach (int num in nums) {
231+
int x = num % k;
232+
for (int j = 0; j < k; ++j) {
233+
int y = (j - x + k) % k;
234+
f[x, y] = f[y, x] + 1;
235+
ans = Math.Max(ans, f[x, y]);
236+
}
237+
}
238+
return ans;
239+
}
240+
}
241+
```
242+
202243
<!-- tabs:end -->
203244

204245
<!-- solution:end -->
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
public class Solution {
2+
public int MaximumLength(int[] nums) {
3+
int k = 2;
4+
int[,] f = new int[k, k];
5+
int ans = 0;
6+
foreach (int num in nums) {
7+
int x = num % k;
8+
for (int j = 0; j < k; ++j) {
9+
int y = (j - x + k) % k;
10+
f[x, y] = f[y, x] + 1;
11+
ans = Math.Max(ans, f[x, y]);
12+
}
13+
}
14+
return ans;
15+
}
16+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
impl Solution {
2+
pub fn maximum_length(nums: Vec<i32>) -> i32 {
3+
let mut f = [[0; 2]; 2];
4+
let mut ans = 0;
5+
for x in nums {
6+
let x = (x % 2) as usize;
7+
for j in 0..2 {
8+
let y = ((j + 2 - x) % 2) as usize;
9+
f[x][y] = f[y][x] + 1;
10+
ans = ans.max(f[x][y]);
11+
}
12+
}
13+
ans
14+
}
15+
}

solution/3200-3299/3202.Find the Maximum Length of Valid Subsequence II/README.md

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,47 @@ function maximumLength(nums: number[], k: number): number {
178178
}
179179
```
180180

181+
#### Rust
182+
183+
```rust
184+
impl Solution {
185+
pub fn maximum_length(nums: Vec<i32>, k: i32) -> i32 {
186+
let k = k as usize;
187+
let mut f = vec![vec![0; k]; k];
188+
let mut ans = 0;
189+
for x in nums {
190+
let x = (x % k as i32) as usize;
191+
for j in 0..k {
192+
let y = (j + k - x) % k;
193+
f[x][y] = f[y][x] + 1;
194+
ans = ans.max(f[x][y]);
195+
}
196+
}
197+
ans
198+
}
199+
}
200+
```
201+
202+
#### C#
203+
204+
```cs
205+
public class Solution {
206+
public int MaximumLength(int[] nums, int k) {
207+
int[,] f = new int[k, k];
208+
int ans = 0;
209+
foreach (int num in nums) {
210+
int x = num % k;
211+
for (int j = 0; j < k; ++j) {
212+
int y = (j - x + k) % k;
213+
f[x, y] = f[y, x] + 1;
214+
ans = Math.Max(ans, f[x, y]);
215+
}
216+
}
217+
return ans;
218+
}
219+
}
220+
```
221+
181222
<!-- tabs:end -->
182223

183224
<!-- solution:end -->

solution/3200-3299/3202.Find the Maximum Length of Valid Subsequence II/README_EN.md

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,47 @@ function maximumLength(nums: number[], k: number): number {
177177
}
178178
```
179179

180+
#### Rust
181+
182+
```rust
183+
impl Solution {
184+
pub fn maximum_length(nums: Vec<i32>, k: i32) -> i32 {
185+
let k = k as usize;
186+
let mut f = vec![vec![0; k]; k];
187+
let mut ans = 0;
188+
for x in nums {
189+
let x = (x % k as i32) as usize;
190+
for j in 0..k {
191+
let y = (j + k - x) % k;
192+
f[x][y] = f[y][x] + 1;
193+
ans = ans.max(f[x][y]);
194+
}
195+
}
196+
ans
197+
}
198+
}
199+
```
200+
201+
#### C#
202+
203+
```cs
204+
public class Solution {
205+
public int MaximumLength(int[] nums, int k) {
206+
int[,] f = new int[k, k];
207+
int ans = 0;
208+
foreach (int num in nums) {
209+
int x = num % k;
210+
for (int j = 0; j < k; ++j) {
211+
int y = (j - x + k) % k;
212+
f[x, y] = f[y, x] + 1;
213+
ans = Math.Max(ans, f[x, y]);
214+
}
215+
}
216+
return ans;
217+
}
218+
}
219+
```
220+
180221
<!-- tabs:end -->
181222

182223
<!-- solution:end -->
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
public class Solution {
2+
public int MaximumLength(int[] nums, int k) {
3+
int[,] f = new int[k, k];
4+
int ans = 0;
5+
foreach (int num in nums) {
6+
int x = num % k;
7+
for (int j = 0; j < k; ++j) {
8+
int y = (j - x + k) % k;
9+
f[x, y] = f[y, x] + 1;
10+
ans = Math.Max(ans, f[x, y]);
11+
}
12+
}
13+
return ans;
14+
}
15+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
impl Solution {
2+
pub fn maximum_length(nums: Vec<i32>, k: i32) -> i32 {
3+
let k = k as usize;
4+
let mut f = vec![vec![0; k]; k];
5+
let mut ans = 0;
6+
for x in nums {
7+
let x = (x % k as i32) as usize;
8+
for j in 0..k {
9+
let y = (j + k - x) % k;
10+
f[x][y] = f[y][x] + 1;
11+
ans = ans.max(f[x][y]);
12+
}
13+
}
14+
ans
15+
}
16+
}

solution/3600-3699/3603.Minimum Cost Path with Alternating Directions II/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,13 @@ tags:
2424

2525
<p>另外给你一个二维整数数组 <code>waitCost</code>,其中 <code>waitCost[i][j]</code> 定义了在该单元格&nbsp;<strong>等待&nbsp;</strong>的成本。</p>
2626

27-
<p>你从第 1 秒开始在单元格 <code>(0, 0)</code>。</p>
27+
<p>路径始终从第 1 步进入单元格 <code>(0, 0)</code>&nbsp;并支付入场花费开始。</p>
2828

2929
<p>每一步,你都遵循交替模式:</p>
3030

3131
<ul>
3232
<li>在&nbsp;<strong>奇数秒&nbsp;</strong>,你必须向&nbsp;<strong>右&nbsp;</strong>或向&nbsp;<strong>下&nbsp;</strong>移动到&nbsp;<strong>相邻&nbsp;</strong>的单元格,并支付其进入成本。</li>
33-
<li>在&nbsp;<strong>偶数秒&nbsp;</strong>,你必须原地&nbsp;<strong>等待&nbsp;</strong>,并支付 <code>waitCost[i][j]</code>。</li>
33+
<li>在&nbsp;<strong>偶数秒&nbsp;</strong>,你必须原地&nbsp;<strong>等待</strong><strong>恰好</strong>&nbsp;1 秒并在 1 秒期间支付 <code>waitCost[i][j]</code>。</li>
3434
</ul>
3535

3636
<p>返回到达 <code>(m - 1, n - 1)</code> 所需的&nbsp;<strong>最小&nbsp;</strong>总成本。</p>

solution/3600-3699/3603.Minimum Cost Path with Alternating Directions II/README_EN.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,13 @@ tags:
2424

2525
<p>You are also given a 2D integer array <code>waitCost</code> where <code>waitCost[i][j]</code> defines the cost to <strong>wait</strong> on that cell.</p>
2626

27-
<p>You start at cell <code>(0, 0)</code> at second 1.</p>
27+
<p>The path will always begin by entering cell <code>(0, 0)</code> on move 1 and paying the entrance cost.</p>
2828

2929
<p>At each step, you follow an alternating pattern:</p>
3030

3131
<ul>
3232
<li>On <strong>odd-numbered</strong> seconds, you must move <strong>right</strong> or <strong>down</strong> to an <strong>adjacent</strong> cell, paying its entry cost.</li>
33-
<li>On <strong>even-numbered</strong> seconds, you must <strong>wait</strong> in place, paying <code>waitCost[i][j]</code>.</li>
33+
<li>On <strong>even-numbered</strong> seconds, you must <strong>wait</strong> in place for <strong>exactly</strong> one second and pay <code>waitCost[i][j]</code> during that second.</li>
3434
</ul>
3535

3636
<p>Return the <strong>minimum</strong> total cost required to reach <code>(m - 1, n - 1)</code>.</p>

0 commit comments

Comments
 (0)