-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution417.cs
64 lines (53 loc) · 1.64 KB
/
Solution417.cs
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
52
53
54
55
56
57
58
59
60
61
62
63
64
using System.Text;
namespace LeetCode.Solutions;
public class Solution417
{
/// <summary>
/// 417. Pacific Atlantic Water Flow - Medium
/// <a href="https://leetcode.com/problems/pacific-atlantic-water-flow">See the problem</a>
/// </summary>
public IList<IList<int>> PacificAtlantic(int[][] heights)
{
var result = new List<IList<int>>();
var rows = heights.Length;
var cols = heights[0].Length;
var pacific = new bool[rows, cols];
var atlantic = new bool[rows, cols];
for (var i = 0; i < rows; i++)
{
Dfs(heights, pacific, i, 0);
Dfs(heights, atlantic, i, cols - 1);
}
for (var i = 0; i < cols; i++)
{
Dfs(heights, pacific, 0, i);
Dfs(heights, atlantic, rows - 1, i);
}
for (var i = 0; i < rows; i++)
{
for (var j = 0; j < cols; j++)
{
if (pacific[i, j] && atlantic[i, j])
{
result.Add(new List<int> { i, j });
}
}
}
return result;
}
private void Dfs(int[][] heights, bool[,] visited, int i, int j)
{
visited[i, j] = true;
var directions = new[] { (0, 1), (0, -1), (1, 0), (-1, 0) };
foreach (var (dx, dy) in directions)
{
var x = i + dx;
var y = j + dy;
if (x < 0 || x >= heights.Length || y < 0 || y >= heights[0].Length || visited[x, y] || heights[x][y] < heights[i][j])
{
continue;
}
Dfs(heights, visited, x, y);
}
}
}