太平洋大西洋水流
「太平洋大西洋水流」問題的詳細指南和 Python 實施。
1. 學習
「太平洋大西洋水流」問題是圖表部分的關鍵挑戰。
此實作著重於 Python 中的簡單層級邏輯。
在我們提供的解決方案中,我們優先考慮技術準確性和程式碼可讀性。
2. Real-World Applications
3. Visual Intuition
可視化太平洋大西洋水流的邏輯流。
4. Prerequisites
5. Step-by-Step Thinking
1. Understand the problem
仔細閱讀太平洋大西洋水流的問題陳述。
2. Formulate brute force
起草一個簡單的迭代解決方案。
3. Identify inefficiency
尋找冗餘計算。
4. Optimize search path
使用散列或排序來加速該過程。
5. Final Implementation
清理生產標準代碼。
問題陳述
有一個 m x n 矩形島嶼,與太平洋和大西洋接壤。太平洋接觸島嶼的左側和頂部邊緣,大西洋接觸島嶼的右側和底部邊緣。
該島被劃分為方形網格。給定一個 m x n 整數矩陣高度,其中 heights[r][c] 表示座標 (r, c) 處單元格的海拔高度。
如果相鄰單元格的高度小於或等於目前單元格的高度,則雨水可以直接流向北、南、東、西的相鄰單元格。水可以從與海洋相鄰的任何細胞流入該海洋。
傳回網格座標結果的二維列表,其中 result[i] = [ri, ci] 表示雨水可以從單元格 (ri, ci) 流向太平洋和大西洋。
寫一個函數 pacificAtlantic(heights: List[List[int]]) -> List[List[int]]。
- •m == len(heights)
- •n == len(heights[0])
- •1 <= m, n <= 200
- •0 <= heights[i][j] <= 10^5
範例
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]]
[[0,4],[1,3],[1,4],[2,2],[3,0],[3,1],[4,0]]
These coordinates can flow to both Pacific (via top/left) and Atlantic (via bottom/right) oceans.
heights = [[1]]
[[0,0]]
The single cell borders both oceans directly.
Need a Hint?
Edge Cases to Watch
- 空輸入結構
- 單元素輸入
- 大數值範圍
準備好解決了嗎?
Open the problem in PyRun's browser-based Python editor. Your code runs fully offline — no server required.
面試見解和變化
複雜度分析分解
為什麼時間: Directly evaluates all possibilities.
為什麼選擇太空: Uses standard local memory.
為什麼時間: Optimized paths reduce total operations.
為什麼選擇太空: May trade memory for speed.
最佳化解決方案Python程式碼
最佳化解決方案Python程式碼
def pacific_atlantic_opt(heights):
ROWS, COLS = len(heights), len(heights[0])
pac, atl = set(), set()
def dfs(r, c, visit, prevHeight):
if ((r, c) in visit or r < 0 or c < 0 or r == ROWS or c == COLS or heights[r][c] < prevHeight):
return
visit.add((r, c))
dfs(r + 1, c, visit, heights[r][c])
dfs(r - 1, c, visit, heights[r][c])
dfs(r, c + 1, visit, heights[r][c])
dfs(r, c - 1, visit, heights[r][c])
for c in range(COLS):
dfs(0, c, pac, heights[0][c])
dfs(ROWS - 1, c, atl, heights[ROWS - 1][c])
for r in range(ROWS):
dfs(r, 0, pac, heights[r][0])
dfs(r, COLS - 1, atl, heights[r][COLS - 1])
res = []
for r in range(ROWS):
for c in range(COLS):
if (r, c) in pac and (r, c) in atl:
res.append([r, c])
return res暴力破解代碼(劇透保護)
暴力破解代碼(劇透保護)
def pacific_atlantic_brute(heights):
res = []
ROWS, COLS = len(heights), len(heights[0])
def can_reach(r, c, target_set):
visited = set()
q = [(r, c)]
visited.add((r, c))
while q:
curr_r, curr_c = q.pop(0)
if (curr_r, curr_c) in target_set: return True
for dr, dc in [[1,0], [-1,0], [0,1], [0,-1]]:
nr, nc = curr_r + dr, curr_c + dc
if 0 <= nr < ROWS and 0 <= nc < COLS and (nr, nc) not in visited and heights[nr][nc] <= heights[curr_r][curr_c]:
visited.add((nr, nc))
q.append((nr, nc))
return False
pac_border = set([(0, c) for c in range(COLS)] + [(r, 0) for r in range(ROWS)])
atl_border = set([(ROWS-1, c) for c in range(COLS)] + [(r, COLS-1) for r in range(ROWS)])
for r in range(ROWS):
for c in range(COLS):
if can_reach(r, c, pac_border) and can_reach(r, c, atl_border):
res.append([r, c])
return resAlgorithm Pattern Checklist
When dealing with Graphs data patterns.
- Are constraints clear?
- Is there a linear or logarithmic optimization possible?
PyRun is built and maintained by an independent solo developer. If this helped your interview prep, consider buying a coffee!
推薦的 Python 資源
透過相關的互動式教學、備忘單和程式碼比較來擴展您的知識。
Python 迴圈:For 與 While 迴圈解釋
了解如何使用 Python 循環來迭代資料。透過互動式範例掌握 for 迴圈、while 迴圈、break、continue 和迴圈最佳實務。
如何在 Python 中對列表進行排序(升序和降序)
了解如何在 Python 中使用 sort() 方法和sorted() 函數對清單進行排序。發現自訂鍵排序和逆序範例。
Python 控制流程備忘單
掌握 Python 中的條件語句、迴圈和循環控制結構。學習 if-else、for、while、break 和 continue。
Python 與 JavaScript:哪種程式語言最好?
Python 和 JavaScript 的全面比較。探索語法差異、效能、用例(後端與前端)和編碼範例。