Skip to content

Commit 7a27dc6

Browse files
authored
Create maximum-manhattan-distance-after-k-changes.py
1 parent 1ab5b92 commit 7a27dc6

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# Time: O(n)
2+
# Space: O(1)
3+
4+
# greedy
5+
class Solution(object):
6+
def maxDistance(self, s, k):
7+
"""
8+
:type s: str
9+
:type k: int
10+
:rtype: int
11+
"""
12+
result = x = y = 0
13+
for i, c in enumerate(s, 1):
14+
if c == 'E':
15+
x += 1
16+
elif c == 'W':
17+
x -= 1
18+
elif c == 'N':
19+
y += 1
20+
elif c == 'S':
21+
y -= 1
22+
result = max(result, min(abs(x)+abs(y)+2*k, i))
23+
return result

0 commit comments

Comments
 (0)