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 i<sup>th</sup>
move, you can choose one of the following directions:
moves[i] = 'L'
or moves[i] = '_'
moves[i] = 'R'
or moves[i] = '_'
Return the distance from the origin of the furthest point you can get to after n
moves.
Example 1:
Input: moves = "L_RL__R"
Output: 3
Explanation: The furthest point we can reach from the origin 0 is point -3 through the following sequence of moves "LLRLLLR".
Example 2:
Input: moves = "_R__LL_"
Output: 5
Explanation: The furthest point we can reach from the origin 0 is point -5 through the following sequence of moves "LRLLLLL".
Example 3:
Input: moves = "_______"
Output: 7
Explanation: The furthest point we can reach from the origin 0 is point 7 through the following sequence of moves "RRRRRRR".
The most straightforward approach is to explore all possible combinations of left and right moves for the underscores and find the maximum distance. This can be done recursively, trying both 'L' and 'R' for each underscore and keeping track of the maximum absolute value of the final position.
def furthest_distance_naive(moves):
n = len(moves)
max_dist = 0
def solve(index, current_pos):
nonlocal max_dist
if index == n:
max_dist = max(max_dist, abs(current_pos))
return
if moves[index] == 'L':
solve(index + 1, current_pos - 1)
elif moves[index] == 'R':
solve(index + 1, current_pos + 1)
else:
solve(index + 1, current_pos - 1) # Try 'L'
solve(index + 1, current_pos + 1) # Try 'R'
solve(0, 0)
return max_dist
# Example Usage
moves1 = "L_RL__R"
print(furthest_distance_naive(moves1)) # Output: 3
moves2 = "_R__LL_"
print(furthest_distance_naive(moves2)) # Output: 5
moves3 = "_______"
print(furthest_distance_naive(moves3)) # Output: 7
The optimal solution leverages the observation that to maximize the distance from the origin, we want to move all underscores either to the left or to the right. We can calculate the distance by assuming all underscores move to the right and then calculate the distance by assuming all underscores move to the left. The maximum of the absolute values of these two distances is the furthest distance possible.
def furthest_distance_optimal(moves):
left_right_moves = 0
underscores = 0
for move in moves:
if move == 'L':
left_right_moves -= 1
elif move == 'R':
left_right_moves += 1
else:
underscores += 1
return max(abs(left_right_moves + underscores), abs(left_right_moves - underscores))
# Example Usage
moves1 = "L_RL__R"
print(furthest_distance_optimal(moves1)) # Output: 3
moves2 = "_R__LL_"
print(furthest_distance_optimal(moves2)) # Output: 5
moves3 = "_______"
print(furthest_distance_optimal(moves3)) # Output: 7
The optimal solution iterates through the string once. Therefore, the runtime complexity is O(n), where n is the length of the moves
string.
The optimal solution uses only a few integer variables, irrespective of the input size. Thus, the space complexity is O(1), i.e., constant space.