Skip to content

Commit a7af00d

Browse files
authored
Merge pull request #3929 from revanth1718/main
Create 1970 - Last Day Where You Can Still Cross.md
2 parents fddf6d2 + ba3cd15 commit a7af00d

File tree

1 file changed

+110
-0
lines changed

1 file changed

+110
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
---
2+
id: last-day-where-you-can-still-cross
3+
title: Last Day Where You Can Still Cross
4+
sidebar_label: Last Day Where You Can Still Cross
5+
tags: [Binary Matrix, BFS, Union Find, Binary Search, C++, Python, Java]
6+
description: Find the last day where it is possible to walk from the top to the bottom of a binary matrix by only walking on land cells.
7+
---
8+
9+
## Problem Statement
10+
11+
### Problem Description
12+
13+
There is a 1-based binary matrix where `0` represents land and `1` represents water. You are given integers `row` and `col` representing the number of rows and columns in the matrix, respectively.
14+
15+
Initially on day `0`, the entire matrix is land. However, each day a new cell becomes flooded with water. You are given a 1-based 2D array `cells`, where `cells[i] = [ri, ci]` represents that on the `i`th day, the cell on the `ri`th row and `ci`th column (1-based coordinates) will be covered with water (i.e., changed to `1`).
16+
17+
You want to find the last day that it is possible to walk from the top to the bottom by only walking on land cells. You can start from any cell in the top row and end at any cell in the bottom row. You can only travel in the four cardinal directions (left, right, up, and down).
18+
19+
Return the last day where it is possible to walk from the top to the bottom by only walking on land cells.
20+
21+
### Example
22+
23+
**Example 1:**
24+
```
25+
Input: row = 2, col = 2, cells = [[1,1],[2,1],[1,2],[2,2]]
26+
Output: 2
27+
```
28+
**Explanation:** The above image depicts how the matrix changes each day starting from day 0.
29+
The last day where it is possible to cross from top to bottom is on day 2.
30+
31+
32+
### Constraints
33+
34+
- `2 <= row, col <= 2 \times 10^4`
35+
- All the values of `cells` are unique.
36+
37+
## Solution
38+
39+
### Intuition
40+
41+
To solve this problem, we can use binary search combined with a BFS (Breadth-First Search) to determine the latest possible day where a valid path exists from the top to the bottom of the matrix. The key idea is to simulate the flooding process in reverse and check if a path exists using BFS.
42+
43+
### Time Complexity and Space Complexity Analysis
44+
45+
- **Time Complexity**: $O((row \times col) \cdot \log(row \times col))$, due to the binary search combined with BFS traversal.
46+
- **Space Complexity**: $O(row \times col)$, for the matrix and auxiliary space used in BFS.
47+
48+
### Code
49+
50+
#### C++
51+
52+
```cpp
53+
#include <vector>
54+
#include <queue>
55+
#include <algorithm>
56+
using namespace std;
57+
58+
class Solution {
59+
public:
60+
int latestDayToCross(int row, int col, vector<vector<int>>& cells) {
61+
vector<vector<int>> grid(row, vector<int>(col, 0));
62+
int left = 0, right = cells.size() - 1;
63+
64+
auto canCross = [&](int day) -> bool {
65+
vector<vector<int>> tempGrid = grid;
66+
for (int i = 0; i <= day; ++i) {
67+
int r = cells[i][0] - 1, c = cells[i][1] - 1;
68+
tempGrid[r][c] = 1;
69+
}
70+
71+
vector<vector<bool>> visited(row, vector<bool>(col, false));
72+
queue<pair<int, int>> q;
73+
74+
for (int c = 0; c < col; ++c) {
75+
if (tempGrid[0][c] == 0) {
76+
q.push({0, c});
77+
visited[0][c] = true;
78+
}
79+
}
80+
81+
while (!q.empty()) {
82+
auto [r, c] = q.front();
83+
q.pop();
84+
85+
if (r == row - 1) return true;
86+
87+
for (const auto& [dr, dc] : vector<pair<int, int>>{{1, 0}, {-1, 0}, {0, 1}, {0, -1}}) {
88+
int nr = r + dr, nc = c + dc;
89+
if (nr >= 0 && nr < row && nc >= 0 && nc < col && !visited[nr][nc] && tempGrid[nr][nc] == 0) {
90+
visited[nr][nc] = true;
91+
q.push({nr, nc});
92+
}
93+
}
94+
}
95+
return false;
96+
};
97+
98+
while (left <= right) {
99+
int mid = left + (right - left) / 2;
100+
if (canCross(mid)) {
101+
left = mid + 1;
102+
} else {
103+
right = mid - 1;
104+
}
105+
}
106+
107+
return right;
108+
}
109+
};
110+
```

0 commit comments

Comments
 (0)