Leetcode 2833: Furthest Point From Origin

grid47
grid47
Exploring patterns and algorithms
Jan 28, 2024 5 min read

You are given a string moves consisting of characters ‘L’, ‘R’, and ‘’. The string represents movements on a number line starting from position 0. You can choose to move left or right when the character is ‘’, and the goal is to calculate the maximum distance from the origin you can reach after completing all the moves.
Problem
Approach
Steps
Complexity
Input: The input consists of a string moves where each character is either 'L', 'R', or '_'.
Example: moves = 'L_RL__R'
Constraints:
• 1 <= moves.length <= 50
• moves consists only of characters 'L', 'R', and '_'.
Output: Return the maximum distance from the origin that can be achieved after completing all the moves.
Example: 3
Constraints:
• The output should be a single integer representing the maximum distance.
Goal: To calculate the maximum distance from the origin after performing all moves, accounting for the choices made at each '_' character.
Steps:
• Initialize two counters to track the furthest left and right positions based on the current moves.
• Iterate over the string moves and calculate the maximum possible distance considering the '_' characters can be either 'L' or 'R'.
• Return the maximum of the absolute values of the two positions.
Goal: The input string moves has constraints as follows:
Steps:
• 1 <= moves.length <= 50
• moves consists only of characters 'L', 'R', and '_'.
Assumptions:
• The characters '_' in the moves string are flexible and can be treated as either 'L' or 'R'.
Input: moves = 'L_RL__R'
Explanation: The furthest point we can reach is -3 after making the sequence of moves 'LLRLLLR'.

Input: moves = '_R__LL_'
Explanation: The furthest point we can reach is -5 after making the sequence 'LRLLLLL'.

Link to LeetCode Lab


LeetCode Solutions Library / DSA Sheets / Course Catalog
comments powered by Disqus