-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy path778-Swim-in-Rising-Water.js
51 lines (41 loc) · 1.05 KB
/
778-Swim-in-Rising-Water.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
// 778. Swim in Rising Water [Hard]
// https://leetcode.com/problems/swim-in-rising-water/
/**
* @param {number[][]} grid
* @return {number}
*/
const swimInWater = (grid) => {
const visited = new Set();
const size = grid.length;
const dfs = (row, col, max) => {
if (
row < 0 ||
row > size - 1 ||
col < 0 ||
col > size - 1 ||
max < grid[row][col] ||
visited.has(size * row + col)
)
return;
visited.add(row * size + col);
dfs(row, col + 1, max);
dfs(row, col - 1, max);
dfs(row + 1, col, max);
dfs(row - 1, col, max);
};
let result = 0;
let low = 0;
let high = size ** 2 - 1;
while (low <= high) {
const middle = Math.floor((low + high) / 2);
dfs(0, 0, middle);
if (visited.has(size ** 2 - 1)) {
high = middle - 1;
result = middle;
} else {
low = middle + 1;
}
visited.clear();
}
return result;
};