[閒聊] 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