[闲聊] LEETCODE 417

楼主: Rushia (みけねこ的鼻屎)   2023-06-25 00:17:35
https://leetcode.com/problems/pacific-atlantic-water-flow/description/
417. Pacific Atlantic Water Flow
给你一个二维阵列 heights ,表示一个矩型岛屿每个座标的高低差,该岛屿下雨的时候
雨水会从高处往低处流,其中岛屿的左上角是太平洋,右下角是大西洋,找出该岛屿的哪
些座标的雨水可以同时流到太平洋和大西洋。
Example 1:
https://assets.leetcode.com/uploads/2021/06/08/waterflow-grid.jpg
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]]
Output: [[0,4],[1,3],[1,4],[2,2],[3,0],[3,1],[4,0]]
Explanation: The following cells can flow to the Pacific and Atlantic oceans,
as shown below:
[0,4]: [0,4] -> Pacific Ocean
[0,4] -> Atlantic Ocean
[1,3]: [1,3] -> [0,3] -> Pacific Ocean
[1,3] -> [1,4] -> Atlantic Ocean
[1,4]: [1,4] -> [1,3] -> [0,3] -> Pacific Ocean
[1,4] -> Atlantic Ocean
[2,2]: [2,2] -> [1,2] -> [0,2] -> Pacific Ocean
[2,2] -> [2,3] -> [2,4] -> Atlantic Ocean
[3,0]: [3,0] -> Pacific Ocean
[3,0] -> [4,0] -> Atlantic Ocean
[3,1]: [3,1] -> [3,0] -> Pacific Ocean
[3,1] -> [4,1] -> Atlantic Ocean
[4,0]: [4,0] -> Pacific Ocean
[4,0] -> Atlantic Ocean
Note that there are other possible paths for these cells to flow to the
Pacific and Atlantic oceans.
Example 2:
Input: heights = [[1]]
Output: [[0,0]]
Explanation: The water can flow from the only cell to the Pacific and
Atlantic oceans.
思路:
1.要判断点能不能走到左上和右下的边边,我们可以从左上和右下出发(共四个行和列)
,如果相邻的点比当前点高,表示下个点可以流到这个点继续往下DFS,这样就可以确
定DFS过后标记的点一定可以流到这个洋。
2.我们只要把左上可以到的点标记成太平洋右下标记成大西洋,最后再检查每个点是否两
个都可以到达就好。
Java Code:
作者: Che31128 (justjoke)   2023-06-25 00:18:00
大师
作者: JIWP (JIWP)   2023-06-25 00:19:00
大师

Links booklink

Contact Us: admin [ a t ] ucptt.com