Leetcode 858: Mirror Reflection

grid47
grid47
Exploring patterns and algorithms
Aug 13, 2024 5 min read

In a special square room with mirrors on all four walls, except for the southwest corner, there are receptors at three other corners, numbered 0, 1, and 2. A laser ray is fired from the southwest corner and hits the east wall at a certain distance from the 0th receptor. Given the side length of the square room, p, and the distance q from the 0th receptor on the east wall where the laser ray meets, return the number of the receptor the ray hits first after bouncing around the room.
Problem
Approach
Steps
Complexity
Input: You are given two integers: p, the length of the side of the square room, and q, the distance from the southwest corner to where the laser first meets the east wall.
Example: Input: p = 4, q = 2
Constraints:
• 1 <= q <= p <= 1000
Output: The function should return an integer representing the receptor number (0, 1, or 2) where the laser ray first reaches after bouncing off the walls.
Example: Output: 1
Constraints:
Goal: The goal is to determine which receptor the laser ray hits first, considering how it reflects off the walls of the square room.
Steps:
• Step 1: Check the divisibility of both p and q by 2 and repeatedly divide them by 2 until one of them is odd.
• Step 2: Use the formula `1 - p%2 + q%2` to determine the receptor number.
• Step 3: Return the receptor number.
Goal: The input values p and q are guaranteed to follow the constraints and the ray will eventually meet a receptor.
Steps:
• p is the length of the square room's sides.
• q is the distance from the southwest corner to the first point where the ray meets the east wall.
• The test cases are guaranteed so that the ray will meet a receptor eventually.
Assumptions:
• The room is always square.
• The input values p and q are valid and guarantee that the ray will eventually meet a receptor.
Input: Input: p = 4, q = 2
Explanation: In this example, the laser first meets the east wall at a distance of 2 from the 0th receptor. After bouncing, the ray reaches receptor 1, as it is reflected back towards the wall.

Link to LeetCode Lab


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