File tree Expand file tree Collapse file tree 3 files changed +91
-0
lines changed
solution/3400-3499/3443.Maximum Manhattan Distance After K Changes Expand file tree Collapse file tree 3 files changed +91
-0
lines changed Original file line number Diff line number Diff 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 -->
Original file line number Diff line number Diff 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 -->
Original file line number Diff line number Diff line change 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+ }
You can’t perform that action at this time.
0 commit comments