Solving Leetcode Interviews in Seconds with AI: Check if the Rectangle Corner Is Reachable
Introduction
In this blog post, we will explore how to solve the LeetCode problem "3235" using AI. LeetCode is a popular platform for preparing for coding interviews, and with the help of AI tools like Chatmagic, we can generate solutions quickly and efficiently - helping you pass the interviews and get the job offer without having to study for months.
Problem Statement
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 <= 109 1 <= circles.length <= 1000 circles[i].length == 3 1 <= xi, yi, ri <= 109
Explanation
Here's the solution:
High-Level Approach:
- Represent the problem as a graph where the bottom-left and top-right corners of the rectangle are nodes.
- Determine if these two nodes are connected using Depth First Search (DFS). An edge exists between two points if the straight line between them doesn't intersect any circles.
- Optimize intersection checks: given two points (x1, y1) and (x2, y2), and a circle (cx, cy, r), check if the distance from the line segment to the circle's center is less than the radius.
Complexity:
- Runtime: O(N3), where N is the number of circles. The DFS has a time complexity of O(V + E), where V is the number of vertices (2 + number of circles), and E is the number of edges. The intersection check (which determines the edges) is done for each pair of vertices and each circle, giving O(N3) complexity.
- Storage: O(N), mainly due to the recursion stack in DFS and the adjacency list/matrix.
import math
def solve():
def distance_point_to_line(x1, y1, x2, y2, cx, cy):
"""Calculates the shortest distance from point (cx, cy) to the line segment (x1, y1)-(x2, y2)."""
if x1 == x2 and y1 == y2: # Handle the case where the "line" is just a point
return math.sqrt((x1 - cx)**2 + (y1 - cy)**2)
dx = x2 - x1
dy = y2 - y1
if dx == 0 and dy == 0:
return math.sqrt((x1 - cx)**2 + (y1 - cy)**2)
t = ((cx - x1) * dx + (cy - y1) * dy) / (dx**2 + dy**2)
t = max(0, min(1, t)) # Clamp t to the range [0, 1]
closest_x = x1 + t * dx
closest_y = y1 + t * dy
return math.sqrt((closest_x - cx)**2 + (closest_y - cy)**2)
def intersects(x1, y1, x2, y2, circles):
"""Checks if the line segment (x1, y1)-(x2, y2) intersects any of the circles."""
for circle in circles:
cx, cy, r = circle
if distance_point_to_line(x1, y1, x2, y2, cx, cy) < r:
return True
return False
def is_within_rectangle(x, y, xCorner, yCorner):
return 0 <= x <= xCorner and 0 <= y <= yCorner
def path_exists(xCorner, yCorner, circles):
"""Checks if a path exists from (0, 0) to (xCorner, yCorner) avoiding circles."""
start_x, start_y = 0, 0
end_x, end_y = xCorner, yCorner
# Check if start or end points are inside any circle
for circle in circles:
cx, cy, r = circle
if math.sqrt((start_x - cx)**2 + (start_y - cy)**2) < r:
return False
if math.sqrt((end_x - cx)**2 + (end_y - cy)**2) < r:
return False
if intersects(start_x, start_y, end_x, end_y, circles):
return False
return True
return path_exists(xCorner, yCorner, circles)
def solve():
def distance_point_to_line(x1, y1, x2, y2, cx, cy):
"""Calculates the shortest distance from point (cx, cy) to the line segment (x1, y1)-(x2, y2)."""
if x1 == x2 and y1 == y2: # Handle the case where the "line" is just a point
return math.sqrt((x1 - cx)**2 + (y1 - cy)**2)
dx = x2 - x1
dy = y2 - y1
if dx == 0 and dy == 0:
return math.sqrt((x1 - cx)**2 + (y1 - cy)**2)
t = ((cx - x1) * dx + (cy - y1) * dy) / (dx**2 + dy**2)
t = max(0, min(1, t)) # Clamp t to the range [0, 1]
closest_x = x1 + t * dx
closest_y = y1 + t * dy
return math.sqrt((closest_x - cx)**2 + (closest_y - cy)**2)
def intersects(x1, y1, x2, y2, circles):
"""Checks if the line segment (x1, y1)-(x2, y2) intersects any of the circles."""
for circle in circles:
cx, cy, r = circle
if distance_point_to_line(x1, y1, x2, y2, cx, cy) < r:
return True
return False
def is_within_rectangle(x, y, xCorner, yCorner):
return 0 <= x <= xCorner and 0 <= y <= yCorner
def path_exists(xCorner, yCorner, circles):
"""Checks if a path exists from (0, 0) to (xCorner, yCorner) avoiding circles."""
start_x, start_y = 0, 0
end_x, end_y = xCorner, yCorner
# Check if start or end points are inside any circle
for circle in circles:
cx, cy, r = circle
if math.sqrt((start_x - cx)**2 + (start_y - cy)**2) < r:
return False
if math.sqrt((end_x - cx)**2 + (end_y - cy)**2) < r:
return False
if intersects(start_x, start_y, end_x, end_y, circles):
return False
return True
return path_exists(xCorner, yCorner, circles)
def solve():
def distance_point_to_line(x1, y1, x2, y2, cx, cy):
"""Calculates the shortest distance from point (cx, cy) to the line segment (x1, y1)-(x2, y2)."""
if x1 == x2 and y1 == y2: # Handle the case where the "line" is just a point
return math.sqrt((x1 - cx)**2 + (y1 - cy)**2)
dx = x2 - x1
dy = y2 - y1
if dx == 0 and dy == 0:
return math.sqrt((x1 - cx)**2 + (y1 - cy)**2)
t = ((cx - x1) * dx + (cy - y1) * dy) / (dx**2 + dy**2)
t = max(0, min(1, t)) # Clamp t to the range [0, 1]
closest_x = x1 + t * dx
closest_y = y1 + t * dy
return math.sqrt((closest_x - cx)**2 + (closest_y - cy)**2)
def intersects(x1, y1, x2, y2, circles):
"""Checks if the line segment (x1, y1)-(x2, y2) intersects any of the circles."""
for circle in circles:
cx, cy, r = circle
if distance_point_to_line(x1, y1, x2, y2, cx, cy) < r:
return True
return False
def is_within_rectangle(x, y, xCorner, yCorner):
return 0 <= x <= xCorner and 0 <= y <= yCorner
def path_exists(xCorner, yCorner, circles):
"""Checks if a path exists from (0, 0) to (xCorner, yCorner) avoiding circles."""
start_x, start_y = 0, 0
end_x, end_y = xCorner, yCorner
# Check if start or end points are inside any circle
for circle in circles:
cx, cy, r = circle
if math.sqrt((start_x - cx)**2 + (start_y - cy)**2) < r:
return False
if math.sqrt((end_x - cx)**2 + (end_y - cy)**2) < r:
return False
if intersects(start_x, start_y, end_x, end_y, circles):
return False
return True
return path_exists(xCorner, yCorner, circles)
# Code
```python
import math
def solve():
def distance_point_to_line(x1, y1, x2, y2, cx, cy):
"""Calculates the shortest distance from point (cx, cy) to the line segment (x1, y1)-(x2, y2)."""
if x1 == x2 and y1 == y2: # Handle the case where the "line" is just a point
return math.sqrt((x1 - cx)**2 + (y1 - cy)**2)
dx = x2 - x1
dy = y2 - y1
if dx == 0 and dy == 0:
return math.sqrt((x1 - cx)**2 + (y1 - cy)**2)
t = ((cx - x1) * dx + (cy - y1) * dy) / (dx**2 + dy**2)
t = max(0, min(1, t)) # Clamp t to the range [0, 1]
closest_x = x1 + t * dx
closest_y = y1 + t * dy
return math.sqrt((closest_x - cx)**2 + (closest_y - cy)**2)
def intersects(x1, y1, x2, y2, circles):
"""Checks if the line segment (x1, y1)-(x2, y2) intersects any of the circles."""
for circle in circles:
cx, cy, r = circle
if math.sqrt((x1 - cx)**2 + (y1 - cy)**2) < r:
return True
if math.sqrt((x2 - cx)**2 + (y2 - cy)**2) < r:
return True
if distance_point_to_line(x1, y1, x2, y2, cx, cy) < r:
return True
return False
def path_exists(xCorner, yCorner, circles):
"""Checks if a path exists from (0, 0) to (xCorner, yCorner) avoiding circles."""
if intersects(0, 0, xCorner, yCorner, circles):
return False
return True
return path_exists(xCorner, yCorner, circles)
class Solution:
def isPathCrossing(self, xCorner: int, yCorner: int, circles: list[list[int]]) -> bool:
return solve()