-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
125 lines (114 loc) · 3.2 KB
/
main.go
File metadata and controls
125 lines (114 loc) · 3.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
// Source: https://leetcode.com/problems/maximum-manhattan-distance-after-k-changes
// Title: Maximum Manhattan Distance After K Changes
// Difficulty: Medium
// Author: Mu Yang <http://muyang.pro>
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// You are given a string `s` consisting of the characters `'N'`, `'S'`, `'E'`, and `'W'`, where `s[i]` indicates movements in an infinite grid:
//
// - `'N'` : Move north by 1 unit.
// - `'S'` : Move south by 1 unit.
// - `'E'` : Move east by 1 unit.
// - `'W'` : Move west by 1 unit.
//
// Initially, you are at the origin `(0, 0)`. You can change **at most** `k` characters to any of the four directions.
//
// Find the **maximum** **Manhattan distance** from the origin that can be achieved **at any time** while performing the movements **in order**.
// The **Manhattan Distance** between two cells `(x_i, y_i)` and `(x_j, y_j)` is `|x_i - x_j| + |y_i - y_j|`.
//
// **Example 1:**
//
// ```
// Input: s = "NWSE", k = 1
// Output: 3
// Explanation:
// Change `s[2]` from `'S'` to `'N'`. The string `s` becomes `"NWNE"`.
//
// Movement | Position (x, y) | Manhattan Distance | Maximum
// ------------------------------------------------------------
// s[0] == 'N' | ( 0, 1) | 0 + 1 = 1 | 1
// s[1] == 'W' | (-1, 1) | 1 + 1 = 2 | 2
// s[2] == 'N' | (-1, 2) | 1 + 2 = 3 | 3
// s[3] == 'E' | ( 0, 2) | 0 + 2 = 2 | 3
//
// The maximum Manhattan distance from the origin that can be achieved is 3. Hence, 3 is the output.
// ```
//
// **Example 2:**
//
// ```
// Input: s = "NSWWEW", k = 3
// Output: 6
// Explanation:
// Change `s[1]` from `'S'` to `'N'`, and `s[4]` from `'E'` to `'W'`. The string `s` becomes `"NNWWWW"`.
// The maximum Manhattan distance from the origin that can be achieved is 6. Hence, 6 is the output.
// ```
//
// **Constraints:**
//
// - `1 <= s.length <= 10^5`
// - `0 <= k <= s.length`
// - `s` consists of only `'N'`, `'S'`, `'E'`, and `'W'`.
//
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
package main
// Loop for 4 possible directions (NE, NW, SE, SW) and try to maximum it
func maxDistance(s string, k int) int {
n := len(s)
dirs := [4][4]byte{
{'N', 'S', 'E', 'W'},
{'N', 'S', 'W', 'E'},
{'S', 'N', 'E', 'W'},
{'S', 'N', 'W', 'E'},
}
ans := 0
for _, dir := range dirs {
N, S, E, W := dir[0], dir[1], dir[2], dir[3]
dist := 0
kk := k
for i := range n {
switch s[i] {
case N, E:
dist++
case S, W:
if kk > 0 {
kk--
dist++
} else {
dist--
}
}
ans = max(ans, dist)
}
}
return ans
}
// Try to maximum each cardinal direction.
// We may flip at most k steps.
// However, the maximum length will not be longer than the number of steps.
func maxDistance2(s string, k int) int {
n := len(s)
var NS, EW int
ans := 0
for i := range n {
switch s[i] {
case 'N':
NS++
case 'S':
NS--
case 'E':
EW++
case 'W':
EW--
}
ns := _abs(NS)
ew := _abs(EW)
ans = max(ans, min(ns+ew+2*k, i+1))
}
return ans
}
func _abs(v int) int {
if v >= 0 {
return v
}
return -v
}