There is a special square room with mirrors on each of the four walls. Except for the southwest corner, there are receptors on each of the remaining corners, numbered 0, 1, and 2.
The square room has walls of length p and a laser ray from the southwest corner first meets the east wall at a distance q from the 0th receptor.
Given the two integers p and q, return the number of the receptor that the ray meets first.
The test cases are guaranteed so that the ray will meet a receptor eventually.
Example 1:
Input: p = 2, q = 1 Output: 2 Explanation: The ray meets receptor 2 the first time it gets reflected back to the left wall.
Example 2:
Input: p = 3, q = 1 Output: 1
Constraints:
1 <= q <= p <= 1000When 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:
We can think of the laser beam as bouncing around a rectangular room until it hits a corner. The brute force way involves tracing the beam's path step by step, calculating each bounce to see where it will hit next. We continue this process until the beam hits one of the corners we're interested in.
Here's how the algorithm would work step-by-step:
def mirror_reflection_brute_force(room_width, room_height):
max_bounces = 1000
current_x = 0
current_y = 0
direction_x = 1
direction_y = 1
for bounce_count in range(max_bounces):
# Calculate time to hit a vertical wall
time_to_vertical = float('inf')
if direction_x == 1:
time_to_vertical = (room_width - current_x)
elif direction_x == -1:
time_to_vertical = current_x
# Calculate time to hit a horizontal wall
time_to_horizontal = float('inf')
if direction_y == 1:
time_to_horizontal = (room_height - current_y)
elif direction_y == -1:
time_to_horizontal = current_y
# Determine which wall is hit first
if time_to_vertical < time_to_horizontal:
current_x = current_x + direction_x * time_to_vertical
current_y = current_y + direction_y * time_to_vertical
current_y = round(current_y, 5)
direction_x *= -1
else:
current_x = current_x + direction_x * time_to_horizontal
current_y = current_y + direction_y * time_to_horizontal
current_x = round(current_x, 5)
direction_y *= -1
# Check if we've reached a target corner
if current_x == room_width and current_y == room_height:
# Top right corner
return 1
if current_x == room_width and current_y == 0:
# Bottom right corner
return 0
if current_x == 0 and current_y == room_height:
# Some other corner
return 2
if current_x == 0 and current_y == 0:
return 2
return 2The core idea is to visualize the reflections as extending the room into a grid of rooms. We keep extending these rooms until a corner where the beam hits is aligned with one of the receivers, then we can use math to figure out which receiver was hit.
Here's how the algorithm would work step-by-step:
def mirror_reflection(room_dimension_p, beam_extension_q):
horizontal_extension = beam_extension_q
vertical_extension = 0
while horizontal_extension % room_dimension_p != 0:
least_common_multiple = room_dimension_p
# Extending until the beam hits a corner.
horizontal_extension += beam_extension_q
vertical_extension += room_dimension_p
horizontal_multiple = horizontal_extension // room_dimension_p
vertical_multiple = vertical_extension // room_dimension_p
# Check if the number of vertical extensions is odd
if vertical_multiple % 2 == 1:
# Check if the number of horizontal extensions is odd
if horizontal_multiple % 2 == 1:
return 1
else:
return 0
else:
#If number of horizontal extension is odd it means it hits receiver 2
if horizontal_multiple % 2 == 1:
return 2
else:
return -1| Case | How to Handle |
|---|---|
| p or q is zero | Return -1 immediately since the mirror is impossible with a zero-sized room dimension. |
| p equals q | If p and q are equal, the ray hits receptor 0 if p is divisible by q and 1 otherwise. |
| Large p and q that lead to potential integer overflow | Use long integer data types for calculations to prevent overflow. |
| The reflection point is exactly at a corner | Handle this case by directly calculating the number of reflections and checking divisibility to determine the receptor. |
| p and q are relatively prime large numbers | Ensure efficient calculation of the least common multiple, avoiding redundant computations. |
| The least common multiple calculations leads to an overflow. | Check for potential overflow during LCM calculation using multiplication with appropriate overflow prevention measures. |
| p and q are both even | Continuously divide both p and q by 2 until at least one of them is odd to simplify the problem and avoid infinite loops. |
| The solution requires very large number of reflections before hitting 0, 1 or 2 | Optimise the computation of reflections to avoid excessive iterations, potentially utilizing mathematical properties to directly jump to the solution. |