LeetCode 3235: Check if the Rectangle Corner Is Reachable Solution
Master LeetCode problem 3235 (Check if the Rectangle Corner Is Reachable), a hard challenge, with our optimized solutions in Java, C++, and Python. Explore detailed explanations, test your code in our interactive editor, and prepare for coding interviews.
3235. Check if the Rectangle Corner Is Reachable
Problem Explanation
Explanation
To solve this problem, we need to determine if there exists a path from the bottom left corner to the top right corner of the rectangle that avoids touching or intersecting any circles, while touching only the two corners of the rectangle.
We can approach this problem by considering the four corners of the rectangle and checking if any of them are reachable. If at least one corner is reachable, then a valid path exists. We can use breadth-first search (BFS) to explore all possible paths from the bottom left corner to the top right corner while avoiding the circles.
Algorithm
- Create a function
isValidPaththat takes the rectangle's dimensions and the list of circles as input. - Initialize a set to store visited points to avoid revisiting them.
- Perform a BFS starting from the bottom left corner of the rectangle.
- At each step, check if the current point is the top right corner of the rectangle. If it is, return true.
- Check if the current point is inside any circle or outside the rectangle. If it is, skip to the next step.
- Add adjacent points (up, right, down, left) to the queue if they are valid (inside the rectangle and not inside any circle).
- Continue the BFS until all possible paths are explored.
- If no valid path is found, return false.
Time Complexity
The time complexity of this algorithm is O(n), where n is the total number of points in the rectangle.
Space Complexity
The space complexity of this algorithm is O(n), where n is the total number of points in the rectangle.
Solution Code
import java.util.*;
class Solution {
public boolean isValidPath(int xCorner, int yCorner, int[][] circles) {
Set<String> visited = new HashSet<>();
Queue<int[]> queue = new LinkedList<>();
queue.offer(new int[]{0, 0});
while (!queue.isEmpty()) {
int[] curr = queue.poll();
int x = curr[0];
int y = curr[1];
if (x == xCorner && y == yCorner) {
return true;
}
if (x < 0 || y < 0 || x > xCorner || y > yCorner) {
continue;
}
boolean insideCircle = false;
for (int[] circle : circles) {
int cx = circle[0];
int cy = circle[1];
int r = circle[2];
if ((x - cx) * (x - cx) + (y - cy) * (y - cy) <= r * r) {
insideCircle = true;
break;
}
}
if (insideCircle) {
continue;
}
String key = x + "," + y;
if (visited.contains(key)) {
continue;
}
visited.add(key);
queue.offer(new int[]{x + 1, y});
queue.offer(new int[]{x - 1, y});
queue.offer(new int[]{x, y + 1});
queue.offer(new int[]{x, y - 1});
}
return false;
}
}Try It Yourself
Loading code editor...
Related LeetCode Problems
Frequently Asked Questions
How to solve LeetCode 3235 (Check if the Rectangle Corner Is Reachable)?
This page provides optimized solutions for LeetCode problem 3235 (Check if the Rectangle Corner Is Reachable) in Java, C++, and Python, along with a detailed explanation and an interactive code editor to test your code.
What is the time complexity of LeetCode 3235 (Check if the Rectangle Corner Is Reachable)?
The time complexity for LeetCode 3235 (Check if the Rectangle Corner Is Reachable) varies by solution. Check the detailed explanation section for specific complexities in Java, C++, and Python implementations.
Can I run code for LeetCode 3235 on DevExCode?
Yes, DevExCode provides an interactive code editor where you can write, test, and run your code for LeetCode 3235 in Java, C++, or Python.