-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy path3443.cpp
46 lines (44 loc) · 1.19 KB
/
3443.cpp
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
class Solution {
public:
int _maxDistance(string& s, int k, char targetX, char targetY) {
int res = 0;
int x = 0;
int y = 0;
for (auto& c : s) {
if (c == 'N' || c == 'S') {
if (c == targetX) x++;
else {
if (k) {
k--;
x++;
}
else {
x--;
}
}
}
if (c == 'E' || c == 'W') {
if (c == targetY) y++;
else {
if (k) {
k--;
y++;
}
else {
y--;
}
}
}
res = max(res, abs(x) + abs(y));
}
return res;
}
int maxDistance(string s, int k) {
int res = 0;
res = max(res, _maxDistance(s, k, 'N', 'W'));
res = max(res, _maxDistance(s, k, 'N', 'E'));
res = max(res, _maxDistance(s, k, 'S', 'W'));
res = max(res, _maxDistance(s, k, 'S', 'E'));
return res;
}
};