[LeetCode] Furthest Point From Origin

2833. Furthest Point From Origin

You are given a string moves of length n consisting only of characters 'L', 'R', and '_'. The string represents your movement on a number line starting from the origin 0.

In the ith move, you can choose one of the following directions:

  • move to the left if moves[i] = 'L' or moves[i] = '_'
  • move to the right if moves[i] = 'R' or moves[i] = '_'

Return the distance from the origin of the furthest point you can get to after n moves.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
class Solution {
public:
int furthestDistanceFromOrigin(string moves) {
int any = 0, at = 0;
for(auto m : moves) {
if(m == 'L') at += 1;
else if(m == 'R') at -= 1;
else any += 1;
}
int res = at;
res = max(res, abs(at - any));
res = max(res, abs(at + any));
return res;
}
};
Author: Song Hayoung
Link: https://songhayoung.github.io/2023/08/27/PS/LeetCode/furthest-point-from-origin/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.