2833. Furthest Point From Origin
MediumView on LeetCode
Advertisement
Intuition
Every '_' wildcard can be assigned to either 'L' or 'R' freely, so they always contribute +1 to the furthest possible distance regardless of the other moves. The non-wildcard moves produce a net displacement of |countL - countR|. Adding all wildcards to that net displacement gives the maximum reachable point.
Algorithm
- 1Make a single pass over the moves string, counting 'L', 'R', and '_' characters.
- 2Compute net = Math.Abs(countL - countR).
- 3Return net + countUnderline (each wildcard extends the furthest point by 1).
Example Walkthrough
Input: moves = "_L__LL__"
- 1.Count: L = 3, R = 0, _ = 5.
- 2.Net displacement without wildcards = |3 - 0| = 3.
- 3.All 5 wildcards go left (dominant direction): 3 + 5 = 8.
Output: 8
Common Pitfalls
- •Do not try to split wildcards between L and R — they always add fully to the dominant side.
- •The answer is |L - R| + wildcards, not max(L, R) + wildcards.
2833.cs
C#
// Approach: Count L moves, R moves, and '_' wildcards separately in one pass.
// The net displacement without wildcards is |countL - countR|. Every wildcard '_'
// can freely move in whichever direction is dominant, adding 1 to the furthest distance.
// So the answer is simply Math.Abs(countL - countR) + countUnderline.
//
// Time: O(N) — single pass over the moves string.
// Space: O(1) — three integer counters only.
public class Solution
{
public int FurthestDistanceFromOrigin(string moves)
{
int countL = 0;
int countR = 0;
int countUnderline = 0;
foreach (char c in moves)
{
if (c == 'L')
++countL;
else if (c == 'R')
++countR;
else // c == '_'
++countUnderline;
}
return Math.Abs(countL - countR) + countUnderline;
}
}Advertisement
Was this solution helpful?