Skip to content

Commit d92950e

Browse files
committed
Sync LeetCode submission Runtime - 2440 ms (83.59%), Memory - 18.2 MB (37.50%)
1 parent a15d519 commit d92950e

File tree

2 files changed

+115
-0
lines changed

2 files changed

+115
-0
lines changed
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
<p>You are given a string <code>s</code> consisting of the characters <code>&#39;N&#39;</code>, <code>&#39;S&#39;</code>, <code>&#39;E&#39;</code>, and <code>&#39;W&#39;</code>, where <code>s[i]</code> indicates movements in an infinite grid:</p>
2+
3+
<ul>
4+
<li><code>&#39;N&#39;</code> : Move north by 1 unit.</li>
5+
<li><code>&#39;S&#39;</code> : Move south by 1 unit.</li>
6+
<li><code>&#39;E&#39;</code> : Move east by 1 unit.</li>
7+
<li><code>&#39;W&#39;</code> : Move west by 1 unit.</li>
8+
</ul>
9+
10+
<p>Initially, you are at the origin <code>(0, 0)</code>. You can change <strong>at most</strong> <code>k</code> characters to any of the four directions.</p>
11+
12+
<p>Find the <strong>maximum</strong> <strong>Manhattan distance</strong> from the origin that can be achieved <strong>at any time</strong> while performing the movements <strong>in order</strong>.</p>
13+
The <strong>Manhattan Distance</strong> between two cells <code>(x<sub>i</sub>, y<sub>i</sub>)</code> and <code>(x<sub>j</sub>, y<sub>j</sub>)</code> is <code>|x<sub>i</sub> - x<sub>j</sub>| + |y<sub>i</sub> - y<sub>j</sub>|</code>.
14+
<p>&nbsp;</p>
15+
<p><strong class="example">Example 1:</strong></p>
16+
17+
<div class="example-block">
18+
<p><strong>Input:</strong> <span class="example-io">s = &quot;NWSE&quot;, k = 1</span></p>
19+
20+
<p><strong>Output:</strong> <span class="example-io">3</span></p>
21+
22+
<p><strong>Explanation:</strong></p>
23+
24+
<p>Change <code>s[2]</code> from <code>&#39;S&#39;</code> to <code>&#39;N&#39;</code>. The string <code>s</code> becomes <code>&quot;NWNE&quot;</code>.</p>
25+
26+
<table style="border: 1px solid black;">
27+
<thead>
28+
<tr>
29+
<th style="border: 1px solid black;">Movement</th>
30+
<th style="border: 1px solid black;">Position (x, y)</th>
31+
<th style="border: 1px solid black;">Manhattan Distance</th>
32+
<th style="border: 1px solid black;">Maximum</th>
33+
</tr>
34+
</thead>
35+
<tbody>
36+
<tr>
37+
<td style="border: 1px solid black;">s[0] == &#39;N&#39;</td>
38+
<td style="border: 1px solid black;">(0, 1)</td>
39+
<td style="border: 1px solid black;">0 + 1 = 1</td>
40+
<td style="border: 1px solid black;">1</td>
41+
</tr>
42+
<tr>
43+
<td style="border: 1px solid black;">s[1] == &#39;W&#39;</td>
44+
<td style="border: 1px solid black;">(-1, 1)</td>
45+
<td style="border: 1px solid black;">1 + 1 = 2</td>
46+
<td style="border: 1px solid black;">2</td>
47+
</tr>
48+
<tr>
49+
<td style="border: 1px solid black;">s[2] == &#39;N&#39;</td>
50+
<td style="border: 1px solid black;">(-1, 2)</td>
51+
<td style="border: 1px solid black;">1 + 2 = 3</td>
52+
<td style="border: 1px solid black;">3</td>
53+
</tr>
54+
<tr>
55+
<td style="border: 1px solid black;">s[3] == &#39;E&#39;</td>
56+
<td style="border: 1px solid black;">(0, 2)</td>
57+
<td style="border: 1px solid black;">0 + 2 = 2</td>
58+
<td style="border: 1px solid black;">3</td>
59+
</tr>
60+
</tbody>
61+
</table>
62+
63+
<p>The maximum Manhattan distance from the origin that can be achieved is 3. Hence, 3 is the output.</p>
64+
</div>
65+
66+
<p><strong class="example">Example 2:</strong></p>
67+
68+
<div class="example-block">
69+
<p><strong>Input:</strong> <span class="example-io">s = &quot;NSWWEW&quot;, k = 3</span></p>
70+
71+
<p><strong>Output:</strong> <span class="example-io">6</span></p>
72+
73+
<p><strong>Explanation:</strong></p>
74+
75+
<p>Change <code>s[1]</code> from <code>&#39;S&#39;</code> to <code>&#39;N&#39;</code>, and <code>s[4]</code> from <code>&#39;E&#39;</code> to <code>&#39;W&#39;</code>. The string <code>s</code> becomes <code>&quot;NNWWWW&quot;</code>.</p>
76+
77+
<p>The maximum Manhattan distance from the origin that can be achieved is 6. Hence, 6 is the output.</p>
78+
</div>
79+
80+
<p>&nbsp;</p>
81+
<p><strong>Constraints:</strong></p>
82+
83+
<ul>
84+
<li><code>1 &lt;= s.length &lt;= 10<sup>5</sup></code></li>
85+
<li><code>0 &lt;= k &lt;= s.length</code></li>
86+
<li><code>s</code> consists of only <code>&#39;N&#39;</code>, <code>&#39;S&#39;</code>, <code>&#39;E&#39;</code>, and <code>&#39;W&#39;</code>.</li>
87+
</ul>
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
class Solution:
2+
def maxDistance(self, s: str, k: int) -> int:
3+
ans = 0
4+
north = south = east = west = 0
5+
for it in s:
6+
if it == "N":
7+
north += 1
8+
elif it == "S":
9+
south += 1
10+
elif it == "E":
11+
east += 1
12+
elif it == "W":
13+
west += 1
14+
times1 = min(north, south, k) # modification times for N and S
15+
times2 = min(
16+
east, west, k - times1
17+
) # modification times for E and W
18+
ans = max(
19+
ans,
20+
self.count(north, south, times1)
21+
+ self.count(east, west, times2),
22+
)
23+
return ans
24+
25+
def count(self, drt1: int, drt2: int, times: int) -> int:
26+
return (
27+
abs(drt1 - drt2) + times * 2
28+
) # Calculate modified Manhattan distance

0 commit comments

Comments
 (0)