LeetCode 778: Swim in Rising Water Solution
Master LeetCode problem 778 (Swim in Rising Water), 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.
778. Swim in Rising Water
Problem Explanation
Explanation:
To solve this problem, we can use a binary search approach combined with a breadth-first search (BFS) to find the earliest time when we can reach the bottom right square. We perform a binary search on the time interval [0, n*n - 1] to find the minimum time needed.
For each time t, we perform a BFS starting from the top left square and explore all valid neighboring squares that can be reached within time t. We mark visited squares to avoid revisiting them. If we can reach the bottom right square during the BFS, we update the minimum time and continue the binary search on the lower half of the time interval. If we cannot reach the target square, we search in the upper half of the time interval.
At the end of the binary search, we will have the minimum time needed to reach the bottom right square.
Time Complexity: The binary search takes O(log(n^2)) = O(log(n)) iterations. Each BFS traversal takes O(n^2) time as we can visit all squares in the worst case. So, the overall time complexity is O(n^2 * log(n)).
Space Complexity: The space complexity is O(n^2) for the queue and visited set used in the BFS traversal.
:
Solution Code
class Solution {
public int swimInWater(int[][] grid) {
int n = grid.length;
int left = 0, right = n * n - 1;
while (left < right) {
int mid = left + (right - left) / 2;
if (canReachTarget(grid, mid, n)) {
right = mid;
} else {
left = mid + 1;
}
}
return left;
}
private boolean canReachTarget(int[][] grid, int time, int n) {
boolean[][] visited = new boolean[n][n];
int[][] dirs = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};
Queue<int[]> queue = new LinkedList<>();
queue.offer(new int[]{0, 0});
visited[0][0] = true;
while (!queue.isEmpty()) {
int[] curr = queue.poll();
int x = curr[0];
int y = curr[1];
if (x == n - 1 && y == n - 1) {
return true;
}
for (int[] dir : dirs) {
int newX = x + dir[0];
int newY = y + dir[1];
if (newX >= 0 && newX < n && newY >= 0 && newY < n && !visited[newX][newY] && grid[newX][newY] <= time) {
queue.offer(new int[]{newX, newY});
visited[newX][newY] = true;
}
}
}
return false;
}
}Try It Yourself
Loading code editor...
Related LeetCode Problems
Frequently Asked Questions
How to solve LeetCode 778 (Swim in Rising Water)?
This page provides optimized solutions for LeetCode problem 778 (Swim in Rising Water) 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 778 (Swim in Rising Water)?
The time complexity for LeetCode 778 (Swim in Rising Water) varies by solution. Check the detailed explanation section for specific complexities in Java, C++, and Python implementations.
Can I run code for LeetCode 778 on DevExCode?
Yes, DevExCode provides an interactive code editor where you can write, test, and run your code for LeetCode 778 in Java, C++, or Python.