LeetCode | 0417. Pacific Atlantic Water Flow太平洋大西洋水流问题【Python】
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了LeetCode | 0417. Pacific Atlantic Water Flow太平洋大西洋水流问题【Python】,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2690字,纯文字阅读大概需要4分钟。
内容图文
![LeetCode | 0417. Pacific Atlantic Water Flow太平洋大西洋水流问题【Python】](/upload/InfoBanner/zyjiaocheng/641/4f30fe695545408790016123de37bc6c.jpg)
LeetCode 0417. Pacific Atlantic Water Flow太平洋大西洋水流问题【Medium】【Python】【DFS】
Problem
Given an m x n
matrix of non-negative integers representing the height of each unit cell in a continent, the "Pacific ocean" touches the left and top edges of the matrix and the "Atlantic ocean" touches the right and bottom edges.
Water can only flow in four directions (up, down, left, or right) from a cell to another one with height equal or lower.
Find the list of grid coordinates where water can flow to both the Pacific and Atlantic ocean.
Note:
- The order of returned grid coordinates does not matter.
- Both m and n are less than 150.
Example:
Given the following 5x5 matrix:
Pacific ~ ~ ~ ~ ~
~ 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 *
* * * * * Atlantic
Return:
[[0, 4], [1, 3], [1, 4], [2, 2], [3, 0], [3, 1], [4, 0]] (positions with parentheses in above matrix).
问题
给定一个 m x n
的非负整数矩阵来表示一片大陆上各个单元格的高度。“太平洋”处于大陆的左边界和上边界,而“大西洋”处于大陆的右边界和下边界。
规定水流只能按照上、下、左、右四个方向流动,且只能从高到低或者在同等高度上流动。
请找出那些水流既可以流动到“太平洋”,又能流动到“大西洋”的陆地单元的坐标。
提示:
- 输出坐标的顺序不重要
- m 和 n 都小于150
示例:
给定下面的 5x5 矩阵:
太平洋 ~ ~ ~ ~ ~
~ 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 *
* * * * * 大西洋
返回:
[[0, 4], [1, 3], [1, 4], [2, 2], [3, 0], [3, 1], [4, 0]] (上图中带括号的单元).
思路
DFS
从边界开始遍历,找到水可以流的位置。
因为是倒过来,所以应该是 [x,y] > [i,j] 才满足,即 [x,y] 应该比 [i,j] 高。
时间复杂度: O(m*n)
空间复杂度: O(m*n)
Python3代码
class Solution:
def pacificAtlantic(self, matrix: List[List[int]]) -> List[List[int]]:
if not matrix or not matrix[0]:
return []
m, n = len(matrix), len(matrix[0])
p_visited = [[False] * n for x in range(m)] # Pacific
a_visited = [[False] * n for x in range(m)] # Atlantic
# left and right
for i in range(m):
self.dfs(p_visited, matrix, m, n, i, 0)
self.dfs(a_visited, matrix, m, n, i, n - 1)
# up and down
for j in range(n):
self.dfs(p_visited, matrix, m, n, 0, j)
self.dfs(a_visited, matrix, m, n, m - 1, j)
ans = []
for i in range(m):
for j in range(n):
if p_visited[i][j] and a_visited[i][j]:
ans.append([i, j])
return ans
def dfs(self, visited, matrix, m, n, i, j):
visited[i][j] = True
directions = [(-1, 0), (0, 1), (1, 0), (0, -1)]
for d in directions:
x, y = i + d[0], j + d[1]
if x < 0 or x >= m or y < 0 or y >= n or visited[x][y] or matrix[x][y] < matrix[i][j]: # [x, y] < [i, j]
continue
self.dfs(visited, matrix, m, n, x, y)
代码地址
内容总结
以上是互联网集市为您收集整理的LeetCode | 0417. Pacific Atlantic Water Flow太平洋大西洋水流问题【Python】全部内容,希望文章能够帮你解决LeetCode | 0417. Pacific Atlantic Water Flow太平洋大西洋水流问题【Python】所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。