Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 9dcd740

Browse files
feat: add rust solution to lc problem: No.3443 (#4509)
No.3443.Maximum Manhattan Distance After K Changes
1 parent fe3eeb4 commit 9dcd740

File tree

3 files changed

+91
-0
lines changed

3 files changed

+91
-0
lines changed

‎solution/3400-3499/3443.Maximum Manhattan Distance After K Changes/README.md‎

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,38 @@ function maxDistance(s: string, k: number): number {
279279
}
280280
```
281281

282+
#### Rust
283+
284+
```rust
285+
impl Solution {
286+
pub fn max_distance(s: String, k: i32) -> i32 {
287+
fn calc(s: &str, a: char, b: char, k: i32) -> i32 {
288+
let mut ans = 0;
289+
let mut mx = 0;
290+
let mut cnt = 0;
291+
for c in s.chars() {
292+
if c == a || c == b {
293+
mx += 1;
294+
} else if cnt < k {
295+
mx += 1;
296+
cnt += 1;
297+
} else {
298+
mx -= 1;
299+
}
300+
ans = ans.max(mx);
301+
}
302+
ans
303+
}
304+
305+
let a = calc(&s, 'S', 'E', k);
306+
let b = calc(&s, 'S', 'W', k);
307+
let c = calc(&s, 'N', 'E', k);
308+
let d = calc(&s, 'N', 'W', k);
309+
a.max(b).max(c).max(d)
310+
}
311+
}
312+
```
313+
282314
<!-- tabs:end -->
283315

284316
<!-- solution:end -->

‎solution/3400-3499/3443.Maximum Manhattan Distance After K Changes/README_EN.md‎

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,38 @@ function maxDistance(s: string, k: number): number {
275275
}
276276
```
277277

278+
#### Rust
279+
280+
```rust
281+
impl Solution {
282+
pub fn max_distance(s: String, k: i32) -> i32 {
283+
fn calc(s: &str, a: char, b: char, k: i32) -> i32 {
284+
let mut ans = 0;
285+
let mut mx = 0;
286+
let mut cnt = 0;
287+
for c in s.chars() {
288+
if c == a || c == b {
289+
mx += 1;
290+
} else if cnt < k {
291+
mx += 1;
292+
cnt += 1;
293+
} else {
294+
mx -= 1;
295+
}
296+
ans = ans.max(mx);
297+
}
298+
ans
299+
}
300+
301+
let a = calc(&s, 'S', 'E', k);
302+
let b = calc(&s, 'S', 'W', k);
303+
let c = calc(&s, 'N', 'E', k);
304+
let d = calc(&s, 'N', 'W', k);
305+
a.max(b).max(c).max(d)
306+
}
307+
}
308+
```
309+
278310
<!-- tabs:end -->
279311

280312
<!-- solution:end -->
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
impl Solution {
2+
pub fn max_distance(s: String, k: i32) -> i32 {
3+
fn calc(s: &str, a: char, b: char, k: i32) -> i32 {
4+
let mut ans = 0;
5+
let mut mx = 0;
6+
let mut cnt = 0;
7+
for c in s.chars() {
8+
if c == a || c == b {
9+
mx += 1;
10+
} else if cnt < k {
11+
mx += 1;
12+
cnt += 1;
13+
} else {
14+
mx -= 1;
15+
}
16+
ans = ans.max(mx);
17+
}
18+
ans
19+
}
20+
21+
let a = calc(&s, 'S', 'E', k);
22+
let b = calc(&s, 'S', 'W', k);
23+
let c = calc(&s, 'N', 'E', k);
24+
let d = calc(&s, 'N', 'W', k);
25+
a.max(b).max(c).max(d)
26+
}
27+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /