You are given two positive integers xCorner and yCorner, and a 2D array circles, where circles[i] = [xi, yi, ri] denotes a circle with center at (xi, yi) and radius ri.
There is a rectangle in the coordinate plane with its bottom left corner at the origin and top right corner at the coordinate (xCorner, yCorner). You need to check whether there is a path from the bottom left corner to the top right corner such that the entire path lies inside the rectangle, does not touch or lie inside any circle, and touches the rectangle only at the two corners.
Return true if such a path exists, and false otherwise.
Example 1:
Input: xCorner = 3, yCorner = 4, circles = [[2,1,1]]
Output: true
Explanation:

The black curve shows a possible path between (0, 0) and (3, 4).
Example 2:
Input: xCorner = 3, yCorner = 3, circles = [[1,1,2]]
Output: false
Explanation:

No path exists from (0, 0) to (3, 3).
Example 3:
Input: xCorner = 3, yCorner = 3, circles = [[2,1,1],[1,2,1]]
Output: false
Explanation:

No path exists from (0, 0) to (3, 3).
Example 4:
Input: xCorner = 4, yCorner = 4, circles = [[5,5,1]]
Output: true
Explanation:

Constraints:
3 <= xCorner, yCorner <= 1091 <= circles.length <= 1000circles[i].length == 31 <= xi, yi, ri <= 109When 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 to this problem is to consider all possible paths from the starting point. We'll repeatedly try moving in all allowed directions until we either reach the target corner or hit a dead end.
Here's how the algorithm would work step-by-step:
def is_rectangle_corner_reachable(
rectangle_width,
rectangle_height,
start_x,
start_y,
move_x,
move_y):
target_x = rectangle_width
target_y = rectangle_height
visited = set()
def explore(current_x, current_y):
# Base case: Reached the target
if current_x == target_x and current_y == target_y:
return True
# Check if the current position has already been visited
if (current_x, current_y) in visited:
return False
visited.add((current_x, current_y))
# Define possible moves
moves = [
(current_x + move_x, current_y),
(current_x - move_x, current_y),
(current_x, current_y + move_y),
(current_x, current_y - move_y)
]
# Iterate through possible moves
for next_x, next_y in moves:
# Validate move:
if 0 <= next_x <= rectangle_width and \
0 <= next_y <= rectangle_height:
# Recursively explore from the new position
if explore(next_x, next_y):
return True
# If no path found from here, backtrack
return False
# Begin search
return explore(start_x, start_y)The core idea is to determine if the destination corner can be reached using only moves that are multiples of given dimensions. This involves understanding a property related to the greatest common divisor (GCD) of the rectangle's dimensions.
Here's how the algorithm would work step-by-step:
def check_if_rectangle_corner_is_reachable(rectangle_width, rectangle_height, corner_x, corner_y): def calculate_greatest_common_divisor(first_number, second_number): while(second_number): first_number, second_number = second_number, first_number % second_number
return first_number
# Calculate the GCD of the rectangle's dimensions.
greatest_common_divisor = calculate_greatest_common_divisor(rectangle_width, rectangle_height)
# Check if the corner coordinates are divisible by the GCD.
if corner_x % greatest_common_divisor == 0 and corner_y % greatest_common_divisor == 0:
return True
else:
return False| Case | How to Handle |
|---|---|
| Rectangle with zero width or height | Return true if both startX and startY are already 0, otherwise false, as the corner is reachable only at the origin. |
| Negative dx or dy values | Take the absolute values of dx and dy since direction doesn't affect reachability. |
| startX and startY are larger than the rectangle dimensions | Reduce startX and startY modulo the rectangle width and height, respectively, to work within bounds. |
| gcd(dx, width) == 0 and startX != 0 | The x coordinate cannot be reached and should return false |
| gcd(dy, height) == 0 and startY != 0 | The y coordinate cannot be reached and should return false |
| Large width, height, dx, dy values (potential overflow) | Use appropriate data types (e.g., long) to avoid integer overflow during calculations of gcd or modulo operations. |
| startX and startY are 0 | Return true immediately as the corner is already at the origin. |
| Width and height are equal to dx and dy, respectively. | Reduce startX and startY using modulo to get the equivalent position within a single repetition of the rectangle's pattern. |