417. Pacific Atlantic Water Flow Medium
1/**
2 * [417] Pacific Atlantic Water Flow
3 *
4 * There is an m x n rectangular island that borders both the Pacific Ocean and Atlantic Ocean. The Pacific Ocean touches the island's left and top edges, and the Atlantic Ocean touches the island's right and bottom edges.
5 * The island is partitioned into a grid of square cells. You are given an m x n integer matrix heights where heights[r][c] represents the height above sea level of the cell at coordinate (r, c).
6 * The island receives a lot of rain, and the rain water can flow to neighboring cells directly north, south, east, and west if the neighboring cell's height is less than or equal to the current cell's height. Water can flow from any cell adjacent to an ocean into the ocean.
7 * Return a 2D list of grid coordinates result where result[i] = [ri, ci] denotes that rain water can flow from cell (ri, ci) to both the Pacific and Atlantic oceans.
8 *
9 * <strong class="example">Example 1:
10 * <img alt="" src="https://assets.leetcode.com/uploads/2021/06/08/waterflow-grid.jpg" style="width: 400px; height: 400px;" />
11 * Input: heights = [[1,2,2,3,5],[3,2,3,4,4],[2,4,5,3,1],[6,7,1,4,5],[5,1,1,2,4]]
12 * Output: [[0,4],[1,3],[1,4],[2,2],[3,0],[3,1],[4,0]]
13 * Explanation: The following cells can flow to the Pacific and Atlantic oceans, as shown below:
14 * [0,4]: [0,4] -> Pacific Ocean
15 * [0,4] -> Atlantic Ocean
16 * [1,3]: [1,3] -> [0,3] -> Pacific Ocean
17 * [1,3] -> [1,4] -> Atlantic Ocean
18 * [1,4]: [1,4] -> [1,3] -> [0,3] -> Pacific Ocean
19 * [1,4] -> Atlantic Ocean
20 * [2,2]: [2,2] -> [1,2] -> [0,2] -> Pacific Ocean
21 * [2,2] -> [2,3] -> [2,4] -> Atlantic Ocean
22 * [3,0]: [3,0] -> Pacific Ocean
23 * [3,0] -> [4,0] -> Atlantic Ocean
24 * [3,1]: [3,1] -> [3,0] -> Pacific Ocean
25 * [3,1] -> [4,1] -> Atlantic Ocean
26 * [4,0]: [4,0] -> Pacific Ocean
27 * [4,0] -> Atlantic Ocean
28 * Note that there are other possible paths for these cells to flow to the Pacific and Atlantic oceans.
29 *
30 * <strong class="example">Example 2:
31 *
32 * Input: heights = [[1]]
33 * Output: [[0,0]]
34 * Explanation: The water can flow from the only cell to the Pacific and Atlantic oceans.
35 *
36 *
37 * Constraints:
38 *
39 * m == heights.length
40 * n == heights[r].length
41 * 1 <= m, n <= 200
42 * 0 <= heights[r][c] <= 10^5
43 *
44 */
45pub struct Solution {}
46
47// problem: https://leetcode.com/problems/pacific-atlantic-water-flow/
48// discuss: https://leetcode.com/problems/pacific-atlantic-water-flow/discuss/?currentPage=1&orderBy=most_votes&query=
49
50// submission codes start here
51
52impl Solution {
53 pub fn pacific_atlantic(heights: Vec<Vec<i32>>) -> Vec<Vec<i32>> {
54 vec![]
55 }
56}
57
58// submission codes end
59
60#[cfg(test)]
61mod tests {
62 use super::*;
63
64 #[test]
65 fn test_417() {
66 }
67}
68
Back
© 2025 bowen.ge All Rights Reserved.