島の数 ---パイセップ--- 「島の数」問題の詳細なガイドと __PYTERM_0__ の実装。 ---パイセップ--- 「1」(陸地) と「0」(水) の地図を表す m x n 2D バイナリ グリッドを指定すると、島の数を返します。 島は水に囲まれており、隣接する土地を水平または垂直に接続して形成されます。グリッドの 4 つの端すべてが水で囲まれていると考えることができます。 関数 __PYCODE_0__ を作成します。 ---パイセップ--- トップ150インタビュー ---パイセップ--- グラフ ---パイセップ--- 「島の数」問題は、グラフ セクションの重要な課題です。 ---パイセップ--- この実装は、__PYTERM_0__ の簡単なレベルのロジックに焦点を当てています。 ---パイセップ--- 当社は、提供するソリューションにおいて技術的な正確さとコードの読みやすさを優先します。 ---パイセップ--- アルゴリズム工学 ---パイセップ--- 競技プログラミング ---パイセップ--- 技術的評価 ---パイセップ--- 島の数のロジック フローを視覚化します。 ---パイセップ--- 島の数の問題文を注意深く読んでください。 ---パイセップ--- 単純な反復ソリューションの草案を作成します。 ---パイセップ--- 冗長な計算を探します。 ---パイセップ--- プロセスを高速化するには、ハッシュまたはソートを使用します。 ---パイセップ--- 実稼働標準に合わせてコードをクリーンアップします。 ---パイセップ--- 空の入力構造体 ---パイセップ--- 単一要素入力 ---パイセップ--- 大きな数値限界 ---パイセップ--- グラフアプローチのロジックを説明してください。 ---パイセップ--- null または空の入力などの特殊なケースについて説明します。 ---パイセップ--- 標準のグラフの問題プロパティが適用されます。 ---パイセップ--- セットやヒープなどのグラフ固有のデータ構造の使用を検討してください。 ---パイセップ--- 島の最大面積 ---パイセップ--- 「島の最大面積」問題の詳細なガイドと __PYTERM_0__ の実装。 ---パイセップ--- m x n のバイナリ行列グリッドが与えられます。島とは、4 方向 (水平または垂直) につながった 1 (陸地を表す) のグループです。グリッドの 4 つの端すべてが水で囲まれていると考えることができます。 島の面積は、島内の値 1 を持つセルの数です。 グリッド内の島の最大面積を返します。島がない場合は0を返します。 関数 __PYCODE_0__ を作成します。 ---パイセップ--- トップ150インタビュー ---パイセップ--- グラフ ---パイセップ--- 「島の最大面積」問題は、グラフ セクションの重要な課題です。 ---パイセップ--- この実装は、__PYTERM_0__ の簡単なレベルのロジックに焦点を当てています。 ---パイセップ--- 当社は、提供するソリューションにおいて技術的な正確さとコードの読みやすさを優先します。 ---パイセップ--- アルゴリズム工学 ---パイセップ--- 競技プログラミング ---パイセップ--- 技術的評価 ---パイセップ--- アイランドの最大面積のロジック フローを視覚化します。 ---パイセップ--- 島の最大面積に関する問題文をよく読んでください。 ---パイセップ--- 単純な反復ソリューションの草案を作成します。 ---パイセップ--- 冗長な計算を探します。 ---パイセップ--- プロセスを高速化するには、ハッシュまたはソートを使用します。
Detailed guide and Python implementation for the 'Number of Islands' problem.
1. 学ぶ
The 'Number of Islands' 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 Number of Islands.
4. Prerequisites
5. Step-by-Step Thinking
1. Understand the problem
Read the problem statement for Number of Islands 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.
問題提起
Given an m x n 2D binary grid grid which represents a map of '1's (land) and '0's (water), return the number of islands.
An island is surrounded by water and is formed by connecting adjacent lands horizontally or vertically. You may assume all four edges of the grid are all surrounded by water.
Write a function numIslands(grid: List[List[str]]) -> int.
- •m == len(grid)
- •n == len(grid[i])
- •1 <= m, n <= 300
- •grid[i][j] is '0' or '1'
例
grid = [["1","1","1","1","0"],["1","1","0","1","0"],["1","1","0","0","0"],["0","0","0","0","0"]]
1
There is a single island consisting of all connected '1's starting from top-left.
grid = [["1","1","0","0","0"],["1","1","0","0","0"],["0","0","1","0","0"],["0","0","0","1","1"]]
3
There are three distinct islands separated by '0's.
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 num_islands_opt(grid: list[list[str]]) -> int:
if not grid: return 0
rows, cols = len(grid), len(grid[0])
islands = 0
def dfs(r, c):
if r < 0 or c < 0 or r >= rows or c >= cols or grid[r][c] == "0":
return
grid[r][c] = "0"
dfs(r + 1, c)
dfs(r - 1, c)
dfs(r, c + 1)
dfs(r, c - 1)
for r in range(rows):
for c in range(cols):
if grid[r][c] == "1":
dfs(r, c)
islands += 1
return islandsブルート フォース コード (スポイラーガード付き)
ブルート フォース コード (スポイラーガード付き)
def num_islands_brute(grid: list[list[str]]) -> int:
if not grid: return 0
rows, cols = len(grid), len(grid[0])
visited = set()
islands = 0
def bfs(r, c):
q = [(r, c)]
visited.add((r, c))
while q:
row, col = q.pop(0)
directions = [[1,0], [-1,0], [0,1], [0,-1]]
for dr, dc in directions:
nr, nc = row + dr, col + dc
if 0 <= nr < rows and 0 <= nc < cols and grid[nr][nc] == "1" and (nr, nc) not in visited:
q.append((nr, nc))
visited.add((nr, nc))
for r in range(rows):
for c in range(cols):
if grid[r][c] == "1" and (r, c) not in visited:
bfs(r, c)
islands += 1
return islandsAlgorithm 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 でリストの長さを調べる方法
Python で len() 関数を使用してリストの長さを調べる方法を学びます。 O(1) の時間計算量とチェック数を理解します。
Python 文字列メソッドのチートシート
Python 文字列操作の完全なリファレンス ガイド。文字列プロパティの書式設定、検索、分割、置換、チェックをマスターします。
Python と JavaScript: どちらのプログラミング言語が最適ですか?
Python と JavaScript の包括的な比較。構文の違い、パフォーマンス、使用例 (バックエンドとフロントエンド)、およびコーディング例を調べます。