From 75b76c8bdf8529ef43d006c6a8c16fa4c908d658 Mon Sep 17 00:00:00 2001 From: philip82148 <92205270+philip82148@users.noreply.github.com> Date: Sat, 8 Feb 2025 23:46:54 +0900 Subject: [PATCH 1/2] add 1-3.cpp --- 31-45/37.Number-of-Islands/1.cpp | 64 ++++++++++++++++++++++++++++++++ 31-45/37.Number-of-Islands/2.cpp | 56 ++++++++++++++++++++++++++++ 31-45/37.Number-of-Islands/3.cpp | 53 ++++++++++++++++++++++++++ 3 files changed, 173 insertions(+) create mode 100644 31-45/37.Number-of-Islands/1.cpp create mode 100644 31-45/37.Number-of-Islands/2.cpp create mode 100644 31-45/37.Number-of-Islands/3.cpp diff --git a/31-45/37.Number-of-Islands/1.cpp b/31-45/37.Number-of-Islands/1.cpp new file mode 100644 index 0000000..0134861 --- /dev/null +++ b/31-45/37.Number-of-Islands/1.cpp @@ -0,0 +1,64 @@ +#if __has_include("../../debug.hpp") +#include "../../debug.hpp" +#endif +// ここまでローカルでのデバッグ用なので気にしないでください -------------------- + +#include +#include + +using namespace std; + +// <時間> +// 11分 +// <感想> +// 競プロで書きまくっているBFS/DFSであるが、エンジニアにとって +// 読みやすいパターンは何か調べなければならない。 +// なお、久しぶりで書くのに時間がかかってしまったが、 +// その他のパターンもいくつか見えている状態なので反復は一旦スキップ +// <他の人のレビューを読んでコメント> +// - row, colの命名について。こういうのが欲しかった。 +// https://github.com/olsen-blue/Arai60/pull/17/files#r1931573071 +// - 4方向のpushをどう処理するか +// https://github.com/katataku/leetcode/pull/16/files#r1905332545 +// - 範囲内の判定をどうするか +// https://github.com/Hurukawa2121/leetcode/pull/17/files#r1898908077 +// - 意外とC++でクロージャ使うのありなんだな +// https://github.com/Hurukawa2121/leetcode/pull/17/files#r1898590021 +// - 競プロで見慣れ過ぎているパターンなので違和感がなかったが、 +// BFS部分は別関数にしないと読みづらいだろうか? +// - walkいいな +// - pairやtupleを使わなくてもいいケース +// https://github.com/colorbox/leetcode/pull/31/files#r1881109902 +// - chromium code searchではvisitedとseenの両方を確認 +// - コーディングに対する根本的な何かをつかんだ気がする +// - pairの場合emplaceもpushも同じ結果になると思っているのだけど、 +// 違うかな。まあemplaceの方が無難か +class Solution { + public: + int numIslands(vector>& grid) { + int num_rows = grid.size(), num_cols = grid[0].size(); + vector> visited(num_rows, vector(num_cols)); + int num_islands = 0; + for (int i = 0; i < num_rows; ++i) { + for (int j = 0; j < num_cols; ++j) { + if (grid[i][j] == '0' || visited[i][j]) continue; + ++num_islands; + + stack> positions; + positions.push({i, j}); + while (!positions.empty()) { + auto [row, col] = positions.top(); + positions.pop(); + if (visited[row][col]) continue; + if (grid[row][col] == '0') continue; + visited[row][col] = true; + if (row + 1 < num_rows) positions.push({row + 1, col}); + if (row - 1 >= 0) positions.push({row - 1, col}); + if (col + 1 < num_cols) positions.push({row, col + 1}); + if (col - 1 >= 0) positions.push({row, col - 1}); + } + } + } + return num_islands; + } +}; diff --git a/31-45/37.Number-of-Islands/2.cpp b/31-45/37.Number-of-Islands/2.cpp new file mode 100644 index 0000000..8a6b904 --- /dev/null +++ b/31-45/37.Number-of-Islands/2.cpp @@ -0,0 +1,56 @@ +#if __has_include("../../debug.hpp") +#include "../../debug.hpp" +#endif +// ここまでローカルでのデバッグ用なので気にしないでください -------------------- + +#include +#include + +using namespace std; + +// <時間> +// 11分 +// <疑問・不安点(・個人的な感想、違う意見があれば教えてほしいもの)> +// - 僕はTypeScriptをよく使っていて、クロージャーが好きである。 +// C++でも使いまくりたいと思っているので、使ってみた。 +// (が、避けた方がいいだろうか) +// - rowとcolが思いっきりshadowingしているけど、 +// 別に良くないかと思ってあえて使っている(良くないか) +class Solution { + public: + int numIslands(vector>& grid) { + int num_rows = grid.size(), num_cols = grid[0].size(); + vector> seen(num_rows, vector(num_cols)); + auto walkIsland = [&](int row, int col) { + stack> positions; + positions.emplace(row, col); + while (!positions.empty()) { + auto [row, col] = positions.top(); + positions.pop(); + if (seen[row][col]) continue; + seen[row][col] = true; + static constexpr pair kDelta[4] = { + {1, 0}, {-1, 0}, {0, 1}, {0, -1}}; + for (auto [d_row, d_col] : kDelta) { + int next_row = row + d_row, next_col = col + d_col; + if (!(0 <= next_row && next_row < num_rows && 0 <= next_col + && next_col < num_cols)) { + continue; + } + if (grid[next_row][next_col] == '0') continue; + positions.emplace(next_row, next_col); + } + } + }; + + int num_islands = 0; + for (int i = 0; i < num_rows; ++i) { + for (int j = 0; j < num_cols; ++j) { + if (grid[i][j] == '0' || seen[i][j]) continue; + walkIsland(i, j); + ++num_islands; + } + } + return num_islands; + } +}; diff --git a/31-45/37.Number-of-Islands/3.cpp b/31-45/37.Number-of-Islands/3.cpp new file mode 100644 index 0000000..575f639 --- /dev/null +++ b/31-45/37.Number-of-Islands/3.cpp @@ -0,0 +1,53 @@ +#if __has_include("../../debug.hpp") +#include "../../debug.hpp" +#endif +// ここまでローカルでのデバッグ用なので気にしないでください -------------------- + +#include +#include + +using namespace std; + +// <時間> +// 6分 +// <疑問・不安点(・個人的な感想、違う意見があれば教えてほしいもの)> +// - dr, dcって伝わるかな +// - new_という命名が悪いとは言わないが、 +// この場合個人的にnew_row, new_colはダサさ(初心者感)を感じるのだが +// 気にし過ぎかな +class Solution { + public: + int numIslands(const vector>& grid) { + int num_rows = grid.size(), num_cols = grid[0].size(); + vector> seen(num_rows, vector(num_cols)); + auto WalkIslands = [&](int start_row, int start_col) { + stack> positions; + positions.emplace(start_row, start_col); + while (!positions.empty()) { + auto [row, col] = positions.top(); + positions.pop(); + if (seen[row][col]) continue; + seen[row][col] = true; + static constexpr pair kDelta[4] = { + {1, 0}, {-1, 0}, {0, 1}, {0, -1}}; + for (auto [dr, dc] : kDelta) { + int new_row = row + dr, new_col = col + dc; + if (0 <= new_row && new_row < num_rows && 0 <= new_col + && new_col < num_cols && grid[new_row][new_col] == '1') { + positions.emplace(new_row, new_col); + } + } + } + }; + + int num_islands = 0; + for (int i = 0; i < num_rows; ++i) { + for (int j = 0; j < num_cols; ++j) { + if (grid[i][j] == '0' || seen[i][j]) continue; + WalkIslands(i, j); + ++num_islands; + } + } + return num_islands; + } +}; From 5a30ab66110160bbfcd91ff1679b076af4e9d307 Mon Sep 17 00:00:00 2001 From: philip82148 <92205270+philip82148@users.noreply.github.com> Date: Sat, 8 Feb 2025 23:50:57 +0900 Subject: [PATCH 2/2] rename 3.cpp -> 2r.cpp --- 31-45/37.Number-of-Islands/{3.cpp => 2r.cpp} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename 31-45/37.Number-of-Islands/{3.cpp => 2r.cpp} (100%) diff --git a/31-45/37.Number-of-Islands/3.cpp b/31-45/37.Number-of-Islands/2r.cpp similarity index 100% rename from 31-45/37.Number-of-Islands/3.cpp rename to 31-45/37.Number-of-Islands/2r.cpp