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:
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".
Constraints:
1 <= moves.length == n <= 50moves consists only of characters 'L', 'R' and '_'.When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
The brute force approach is like trying every single possible path to find the furthest point. We will calculate the distance of every point from the origin. Then we will pick the point with the largest distance.
Here's how the algorithm would work step-by-step:
def find_furthest_point_brute_force(points):
furthest_distance = 0
furthest_point = None
for point in points:
# Calculate the distance from the origin.
distance_from_origin = (point[0]**2 + point[1]**2)**0.5
# Update furthest_distance if current point is further.
if distance_from_origin > furthest_distance:
furthest_distance = distance_from_origin
furthest_point = point
return furthest_pointThe goal is to find the point furthest from the origin. The clever idea is to only focus on the extreme values of the inputs since distance increases as the absolute values increase. We need to identify the largest possible x and y values, considering both positive and negative.
Here's how the algorithm would work step-by-step:
def furthest_point_from_origin(points):
max_x_value = float('-inf')
min_x_value = float('inf')
max_y_value = float('-inf')
min_y_value = float('inf')
for point in points:
x_coordinate, y_coordinate = point
max_x_value = max(max_x_value, x_coordinate)
min_x_value = min(min_x_value, x_coordinate)
max_y_value = max(max_y_value, y_coordinate)
min_y_value = min(min_y_value, y_coordinate)
# Calculate the distances from the origin for all combinations
distance_1 = (max_x_value**2 + max_y_value**2)**0.5
distance_2 = (max_x_value**2 + min_y_value**2)**0.5
distance_3 = (min_x_value**2 + max_y_value**2)**0.5
distance_4 = (min_x_value**2 + min_y_value**2)**0.5
# Compare distances and get the largest distance
max_distance = max(distance_1, distance_2, distance_3, distance_4)
return max_distance
def main():
points_list = [[1, 2], [-1, -2], [3, 4], [-5, -6]]
#Find the coordinate farthest from the origin
max_distance_point = furthest_point_from_origin(points_list)
print(f'{max_distance_point=}')
if __name__ == "__main__":
main()| Case | How to Handle |
|---|---|
| Empty input list | Return 0, indicating the origin (0,0) is the furthest point since no other points exist. |
| List contains only the origin (0,0) | Return 0, as the furthest point is still the origin itself. |
| List contains very large positive and negative numbers | Use appropriate data types (e.g., long or double) to prevent integer overflow when calculating distance from origin. |
| List contains duplicate points | The algorithm should correctly calculate the distance for each unique point, treating duplicates as separate occurrences for distance calculation. |
| All points are equidistant from the origin | Return any one of the points since they are all equally furthest. |
| List contains only one point | Return the distance of that single point from origin. |
| Input list is null | Throw an IllegalArgumentException or return 0, depending on the specified error handling policy. |
| List contains floating-point numbers with limited precision | Be aware that floating-point comparisons can be imprecise, and consider using a small tolerance when comparing distances to avoid errors. |