太平洋大西洋の水の流れ ---パイセップ--- 「太平洋大西洋水流」問題の詳細なガイドと __PYTERM_0__ の実装。 ---パイセップ--- 太平洋と大西洋に接する m × n 個の長方形の島があります。太平洋は島の左端と上端に接しており、大西洋は島の右端と下端に接しています。 アイランドは正方形のセルのグリッドに分割されます。 m x n の整数行列の高さが与えられます。heights[r][c] は、座標 (r, c) のセルの海面からの高さを表します。 隣接するセルの高さが現在のセルの高さ以下である場合、雨水は隣接するセルに真北、南、東、西に流れることができます。水は、海に隣接する任意のセルからその海に流れることができます。 グリッド座標結果の 2D リストを返します。result[i] = [ri, ci] は、雨水がセル (ri, ci) から太平洋と大西洋の両方に流れる可能性があることを示します。 関数 __PYCODE_0__ を作成します。 ---パイセップ--- トップ150インタビュー ---パイセップ--- グラフ ---パイセップ--- 「太平洋の大西洋の水の流れ」問題は、グラフ セクションの重要な課題です。 ---パイセップ--- この実装は、__PYTERM_0__ の簡単なレベルのロジックに焦点を当てています。 ---パイセップ--- 当社は、提供するソリューションにおいて技術的な正確さとコードの読みやすさを優先します。 ---パイセップ--- アルゴリズム工学 ---パイセップ--- 競技プログラミング ---パイセップ--- 技術的評価 ---パイセップ--- 太平洋大西洋水流のロジック フローを視覚化します。 ---パイセップ--- Pacific Atlantic Water Flow の問題文を注意深く読んでください。 ---パイセップ--- 単純な反復ソリューションの草案を作成します。 ---パイセップ--- 冗長な計算を探します。 ---パイセップ--- プロセスを高速化するには、ハッシュまたはソートを使用します。 ---パイセップ--- 実稼働標準に合わせてコードをクリーンアップします。 ---パイセップ--- 空の入力構造体 ---パイセップ--- 単一要素入力 ---パイセップ--- 大きな数値限界 ---パイセップ--- グラフアプローチのロジックを説明してください。 ---パイセップ--- null または空の入力などの特殊なケースについて説明します。 ---パイセップ--- 標準のグラフの問題プロパティが適用されます。 ---パイセップ--- セットやヒープなどのグラフ固有のデータ構造の使用を検討してください。 ---パイセップ--- 囲まれた地域 ---パイセップ--- 「囲まれた領域」問題の詳細なガイドと __PYTERM_0__ 実装。 ---パイセップ--- 「X」と「O」を含む m x n マトリックス ボードがあるとすると、「X」で 4 方向に囲まれたすべての領域をキャプチャします。 領域は、囲まれた領域内のすべての「O」を「X」に反転することでキャプチャされます。 変更されたボードを返す関数 __PYCODE_0__ を作成します。 ---パイセップ--- トップ150インタビュー ---パイセップ--- グラフ ---パイセップ--- 「囲まれた領域」問題は、グラフ セクションの重要な課題です。 ---パイセップ--- この実装は、__PYTERM_0__ の簡単なレベルのロジックに焦点を当てています。 ---パイセップ--- 当社は、提供するソリューションにおいて技術的な正確さとコードの読みやすさを優先します。 ---パイセップ--- アルゴリズム工学 ---パイセップ--- 競技プログラミング ---パイセップ--- 技術的評価 ---パイセップ--- 囲まれた領域のロジック フローを視覚化します。 ---パイセップ--- 囲まれた領域の問題ステートメントを注意深く読んでください。 ---パイセップ--- 単純な反復ソリューションの草案を作成します。 ---パイセップ--- 冗長な計算を探します。 ---パイセップ--- プロセスを高速化するには、ハッシュまたはソートを使用します。
Detailed guide and Python implementation for the 'Pacific Atlantic Water Flow' problem.
1. 学ぶ
The 'Pacific Atlantic Water Flow' problem is a key challenge in the Graphs section.
This implementation focuses on easy-level logic in Python.
We prioritize technical accuracy and code readability in our provided solutions.
2. Real-World Applications
3. Visual Intuition
Visualizing the logic flow for Pacific Atlantic Water Flow.
4. Prerequisites
5. Step-by-Step Thinking
1. Understand the problem
Read the problem statement for Pacific Atlantic Water Flow carefully.
2. Formulate brute force
Draft a simple iterative solution.
3. Identify inefficiency
Look for redundant calculations.
4. Optimize search path
Use hashing or sorting to speed up the process.
5. Final Implementation
Clean up the code for production standards.
問題提起
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.
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).
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 that ocean.
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.
Write a function 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
- Empty input structures
- Single element inputs
- Large numerical bounds
解決する準備はできましたか?
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?
Key Revision Notes
Standard Graphs problem properties apply.
関連する質問
PyRun is built and maintained by an independent solo developer. If this helped your interview prep, consider buying a coffee!
推奨される Python リソース
関連するインタラクティブなチュートリアル、チートシート、コード比較で知識を深めてください。
Python ループ
Python ループを使用してデータを反復処理する方法を学びます。インタラクティブな例を使用して、for ループ、while ループ、ブレーク、継続、ループのベスト プラクティスをマスターします。
Python でリストを並べ替える方法
sort() メソッドとsorted() 関数を使用して、Python でリストを並べ替える方法を学びます。カスタムキーの並べ替えと逆順の例をご覧ください。
Python 制御フローのチートシート
Python の条件文、ループ、ループ制御構造をマスターします。 if-else、for、while、break、Continue を学びましょう。
Python と JavaScript: どちらのプログラミング言語が最適ですか?
Python と JavaScript の包括的な比較。構文の違い、パフォーマンス、使用例 (バックエンドとフロントエンド)、およびコーディング例を調べます。