From 09623e05e45e9a805ee11f9094939a851fc9c018 Mon Sep 17 00:00:00 2001 From: busker <165013324+hiroki-horiguchi-dev@users.noreply.github.com> Date: Thu, 21 May 2026 00:25:17 +0900 Subject: [PATCH] Update 200.md --- graph-bfs-dns/200.md | 133 +++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 129 insertions(+), 4 deletions(-) diff --git a/graph-bfs-dns/200.md b/graph-bfs-dns/200.md index 7d81095..fffc91e 100644 --- a/graph-bfs-dns/200.md +++ b/graph-bfs-dns/200.md @@ -1,6 +1,131 @@ -- 問題: []() -- コメント集: [](https://docs.google.com/document/d/11HV35ADPo9QxJOpJQ24FcZvtvioli770WWdZZDaLOfg/mobilebasic#h.o0jquy48e6cy) +- 問題: [200. Number of Islands](https://leetcode.com/problems/number-of-islands/submissions/2008160365/) +- コメント集: [200. Number of Islands](https://docs.google.com/document/d/11HV35ADPo9QxJOpJQ24FcZvtvioli770WWdZZDaLOfg/mobilebasic#h.o0jquy48e6cy) + - 一応全て確認したが、Python関係の話が多かったと思う + - コメント先のPullRequestで、column, row の変数名の話と引数を変更するのはどうなんだ?という話があったので考えてみた - 条件 + - m == grid.length + - n == grid[i].length + - 1 <= m, n <= 300 + - grid[i][j] is '0' or '1'. - 方針1 -- 時間計算量: -- 空間計算量: + - grid に対して二重ループを回しながら1件ずつ走査する + - 走査の過程で、`grid[column][row]` が land の場合、隣接する land を全て water に変更するような処理を再帰関数(dfs)で組む + - DFSのイメージ: 行けるところまで深く潜って戻ってきてね + - DFSのユースケース: 全部探索, `find`コマンドで目的のファイルを探すなどが該当する +- 方針2 + - grid に対して二重ループを回しながら1件ずつ走査する + - 基本的にはDFSと同じだが、`queue`を使って波紋が広がるように調べていく点が少し違う + - BFSのイメージ: 波紋が広がるように、起点から調べていく. DFSとの差分でいうと、最初に到達した探索結果が必ず最短が保証されている.なぜかというと、Queueに順序付きQueueを使っており、挿入した順序が必ず守られているから + - BFSのユースケース: マップの最短経路探索 +- 時間計算量: `O(m*n)` +- 空間計算量: `O(m*n)` --> 最悪、全ての要素がコールスタックに乗ることになる, BFSは全ての要素がQueueにのることになる + +## DFS +- 20分 +```java +class Solution { + public int numIslands(char[][] grid) { + if (grid.length == 0) return 0; + + int result = 0; + for (int rowIndex = 0; rowIndex < grid.length; rowIndex++) { + for (int columnIndex = 0; columnIndex < grid[0].length; columnIndex++) { + result += numIslandsHelper(grid, rowIndex, columnIndex); + } + } + + return result; + } + + private int numIslandsHelper(char[][] grid, int rowIndex, int columnIndex) { + // index ouf of range + if ((rowIndex < 0 || columnIndex < 0) || (rowIndex >= grid.length || columnIndex >= grid[0].length)) { + return 0; + } + // find water + if (grid[rowIndex][columnIndex] == '0') { + return 0; + } + // find land + if (grid[rowIndex][columnIndex] == '1') { + grid[rowIndex][columnIndex] = '0'; + // seach top, left, right, bottom of this land + // find top + numIslandsHelper(grid, rowIndex - 1, columnIndex); + // find bottom + numIslandsHelper(grid, rowIndex + 1, columnIndex); + // find left + numIslandsHelper(grid, rowIndex, columnIndex - 1); + // find right + numIslandsHelper(grid, rowIndex, columnIndex + 1); + + return 1; + } + + throw new IllegalArgumentException("The argument grid[][] contained a character other than 0 or 1"); + } +} +``` + +## BFS +```java +class Solution { + public int numIslands(char[][] grid) { + if (grid.length == 0) return 0; + + int result = 0; + for (int rowIndex = 0; rowIndex < grid.length; rowIndex++) { + for (int columnIndex = 0; columnIndex < grid[0].length; columnIndex++) { + result += numIslandsHelper(grid, rowIndex, columnIndex); + } + } + + return result; + } + + private int numIslandsHelper(char[][] grid, int rowIndex, int columnIndex) { + Queue lands = new ArrayDeque<>(); + // find land + if (grid[rowIndex][columnIndex] == '1') { + grid[rowIndex][columnIndex] = '0'; + lands.add(new int[]{rowIndex, columnIndex}); + } else { + return 0; + } + + while (!lands.isEmpty()) { + int[] index = lands.poll(); + int numRow = index[0]; + int numColumn = index[1]; + + // 上下左右を探索して、1ならqueueにつめる + int nextNumRow = numRow + 1; + int previousNumRow = numRow - 1; + int nextNumColumn = numColumn + 1; + int previousNumColumn = numColumn - 1; + // top + if (previousNumRow >= 0 && grid[previousNumRow][numColumn] == '1') { + grid[previousNumRow][numColumn] = '0'; + lands.add(new int[]{previousNumRow, numColumn}); + } + // bottom + if (nextNumRow < grid.length && grid[nextNumRow][numColumn] == '1') { + grid[nextNumRow][numColumn] = '0'; + lands.add(new int[]{nextNumRow, numColumn}); + } + // left + if (previousNumColumn >= 0 && grid[numRow][previousNumColumn] == '1') { + grid[numRow][previousNumColumn] = '0'; + lands.add(new int[]{numRow, previousNumColumn}); + } + // right + if (nextNumColumn < grid[0].length && grid[numRow][nextNumColumn] == '1') { + grid[numRow][nextNumColumn] = '0'; + lands.add(new int[]{numRow, nextNumColumn}); + } + } + + return 1; + } +} +``` \ No newline at end of file