Skip to content

Commit 1a9eeb6

Browse files
committed
Add solution #3443
1 parent 48637c4 commit 1a9eeb6

File tree

2 files changed

+53
-1
lines changed

2 files changed

+53
-1
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# 2,250+ LeetCode solutions in JavaScript
1+
# 2,300+ LeetCode solutions in JavaScript
22

33
[https://leetcodejavascript.com](https://leetcodejavascript.com)
44

@@ -2300,6 +2300,7 @@
23002300
3405|[Count the Number of Arrays with K Matching Adjacent Elements](./solutions/3405-count-the-number-of-arrays-with-k-matching-adjacent-elements.js)|Hard|
23012301
3423|[Maximum Difference Between Adjacent Elements in a Circular Array](./solutions/3423-maximum-difference-between-adjacent-elements-in-a-circular-array.js)|Easy|
23022302
3442|[Maximum Difference Between Even and Odd Frequency I](./solutions/3442-maximum-difference-between-even-and-odd-frequency-i.js)|Easy|
2303+
3443|[Maximum Manhattan Distance After K Changes](./solutions/3443-maximum-manhattan-distance-after-k-changes.js)|Medium|
23032304
3445|[Maximum Difference Between Even and Odd Frequency II](./solutions/3445-maximum-difference-between-even-and-odd-frequency-ii.js)|Hard|
23042305
3452|[Sum of Good Numbers](./solutions/3452-sum-of-good-numbers.js)|Easy|
23052306
3461|[Check If Digits Are Equal in String After Operations I](./solutions/3461-check-if-digits-are-equal-in-string-after-operations-i.js)|Easy|
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/**
2+
* 3443. Maximum Manhattan Distance After K Changes
3+
* https://leetcode.com/problems/maximum-manhattan-distance-after-k-changes/
4+
* Difficulty: Medium
5+
*
6+
* You are given a string s consisting of the characters 'N', 'S', 'E', and 'W', where s[i]
7+
* indicates movements in an infinite grid:
8+
* - 'N' : Move north by 1 unit.
9+
* - 'S' : Move south by 1 unit.
10+
* - 'E' : Move east by 1 unit.
11+
* - 'W' : Move west by 1 unit.
12+
*
13+
* Initially, you are at the origin (0, 0). You can change at most k characters to any of the
14+
* four directions.
15+
*
16+
* Find the maximum Manhattan distance from the origin that can be achieved at any time while
17+
* performing the movements in order.
18+
*
19+
* The Manhattan Distance between two cells (xi, yi) and (xj, yj) is |xi - xj| + |yi - yj|.
20+
*/
21+
22+
/**
23+
* @param {string} s
24+
* @param {number} k
25+
* @return {number}
26+
*/
27+
var maxDistance = function(s, k) {
28+
const directions = {
29+
'N': [0, 1],
30+
'S': [0, -1],
31+
'E': [1, 0],
32+
'W': [-1, 0]
33+
};
34+
35+
let x = 0;
36+
let y = 0;
37+
let result = 0;
38+
39+
for (let i = 0; i < s.length; i++) {
40+
const [dx, dy] = directions[s[i]];
41+
x += dx;
42+
y += dy;
43+
44+
const currentDistance = Math.abs(x) + Math.abs(y);
45+
const maxPossibleExtra = Math.min(2 * k, i + 1 - currentDistance);
46+
47+
result = Math.max(result, currentDistance + maxPossibleExtra);
48+
}
49+
50+
return result;
51+
};

0 commit comments

Comments
 (0)