Skip to content

Create 3443. Maximum Manhattan Distance After K Changes #820

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 20, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions 3443. Maximum Manhattan Distance After K Changes
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
class Solution {
public:
int maxDistance(string s, int k) {
int dist=0, N=0, S=0, E=0, W=0, n=s.size();

for(int i=0; i<n; i++){

if(s[i]=='N'){
N++;
}else if(s[i]=='S'){
S++;
}else if(s[i]=='W'){
W++;
}else{
E++;
}

int manhattan_dist=abs(N-S)+abs(E-W);
int opposite_steps=min(N, S)+min(E, W);
int flips=min(k, opposite_steps);
int new_dist=manhattan_dist+2*flips;
dist=max(dist, new_dist);
}

return dist;
}
};
Loading