From d4e9bd44455370d7ab0b0d672739633db6086522 Mon Sep 17 00:00:00 2001 From: philip82148 <92205270+philip82148@users.noreply.github.com> Date: Sat, 8 Feb 2025 01:09:52 +0900 Subject: [PATCH 1/6] add 1.cpp --- 31-45/32.Unique-Paths-II/1.cpp | 62 ++++++++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100644 31-45/32.Unique-Paths-II/1.cpp diff --git a/31-45/32.Unique-Paths-II/1.cpp b/31-45/32.Unique-Paths-II/1.cpp new file mode 100644 index 0000000..d8da90b --- /dev/null +++ b/31-45/32.Unique-Paths-II/1.cpp @@ -0,0 +1,62 @@ +#include +#if __has_include("../../debug.hpp") +#include "../../debug.hpp" +#endif +// ここまでローカルでのデバッグ用なので気にしないでください -------------------- + +#include + +using namespace std; + +// <時間> +// 13分 +// <感想> +// 直前にUnique Pathsを解いていたので早く解けた。 +// と思っていたらnum_paths[0][*]やnum_paths[*][0]の初期化処理が違うことに気づかず +// バグった。 +// <コメント> +// - 他の人のPRを見ていてnum_paths[0][*]やnum_paths[*][0]の初期化処理を +// 途中でbreakさせることが出来ることに気づいた。 +// でも個人的にはこっちの方が(一貫性があってDPとしての)ロジックが分かりやすいかなぁと。 +// - ロジックに問題があるわけではないので反復はレビューをもらってからしたい。 +// <懸念点> +// - L49/L52とL56-L57のifに統一性が無いが、フォーマットするときの行幅の関係上、 +// 統一すると行数が増えてしまうのでこうしている。 +// (L43をif(!obstacle...にすると{}でくくる必要があり、3行に増える) +// が、あんまりよくないだろうか?(良くない気がするが聞いてみたい) +// (行数は無駄に増やさない方が読みやすいと思っている) +// - 書いている時は気づかなかったが行列(二次元配列)にwidthやheightという単語は +// あんまりしっくり来ておらず、避けていたことに気づいた。 +// 気にしすぎでしょうか。(普段はnum_colsやnum_rows等) +// +// +// +// +// +// +// +// +// L39(行数調整用コメント) +class Solution { + public: + int uniquePathsWithObstacles(vector>& obstacleGrid) { + if (obstacleGrid[0][0]) return 0; + + int height = obstacleGrid.size(), width = obstacleGrid[0].size(); + vector> num_paths(height, vector(width)); + num_paths[0][0] = 1; + for (int i = 1; i < height; ++i) { + if (!obstacleGrid[i][0]) num_paths[i][0] = num_paths[i - 1][0]; + } + for (int i = 1; i < width; ++i) { + if (!obstacleGrid[0][i]) num_paths[0][i] = num_paths[0][i - 1]; + } + for (int i = 1; i < height; ++i) { + for (int j = 1; j < width; ++j) { + if (obstacleGrid[i][j]) continue; + num_paths[i][j] = num_paths[i - 1][j] + num_paths[i][j - 1]; + } + } + return num_paths[height - 1][width - 1]; + } +}; From bafbbba05153595ace590a84dd9245d078cd4d01 Mon Sep 17 00:00:00 2001 From: philip82148 <92205270+philip82148@users.noreply.github.com> Date: Sat, 8 Feb 2025 01:13:02 +0900 Subject: [PATCH 2/6] remove unused header --- 31-45/32.Unique-Paths-II/1.cpp | 1 - 1 file changed, 1 deletion(-) diff --git a/31-45/32.Unique-Paths-II/1.cpp b/31-45/32.Unique-Paths-II/1.cpp index d8da90b..59c4364 100644 --- a/31-45/32.Unique-Paths-II/1.cpp +++ b/31-45/32.Unique-Paths-II/1.cpp @@ -1,4 +1,3 @@ -#include #if __has_include("../../debug.hpp") #include "../../debug.hpp" #endif From fe7b5bc35fcf9b2a8c2ea756e3724572faa91239 Mon Sep 17 00:00:00 2001 From: philip82148 <92205270+philip82148@users.noreply.github.com> Date: Sat, 8 Feb 2025 01:13:33 +0900 Subject: [PATCH 3/6] tweak --- 31-45/32.Unique-Paths-II/1.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/31-45/32.Unique-Paths-II/1.cpp b/31-45/32.Unique-Paths-II/1.cpp index 59c4364..d37b0ef 100644 --- a/31-45/32.Unique-Paths-II/1.cpp +++ b/31-45/32.Unique-Paths-II/1.cpp @@ -35,6 +35,7 @@ using namespace std; // // // +// // L39(行数調整用コメント) class Solution { public: From 2f471dc2122152a0aaaa6f8b373bf1dd20ab18c1 Mon Sep 17 00:00:00 2001 From: philip82148 <92205270+philip82148@users.noreply.github.com> Date: Sat, 8 Feb 2025 01:48:18 +0900 Subject: [PATCH 4/6] fix comment --- 31-45/32.Unique-Paths-II/1.cpp | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/31-45/32.Unique-Paths-II/1.cpp b/31-45/32.Unique-Paths-II/1.cpp index d37b0ef..7c4803a 100644 --- a/31-45/32.Unique-Paths-II/1.cpp +++ b/31-45/32.Unique-Paths-II/1.cpp @@ -19,9 +19,9 @@ using namespace std; // でも個人的にはこっちの方が(一貫性があってDPとしての)ロジックが分かりやすいかなぁと。 // - ロジックに問題があるわけではないので反復はレビューをもらってからしたい。 // <懸念点> -// - L49/L52とL56-L57のifに統一性が無いが、フォーマットするときの行幅の関係上、 +// - L50/L53とL57-L58のifに統一性が無いが、フォーマットするときの行幅の関係上、 // 統一すると行数が増えてしまうのでこうしている。 -// (L43をif(!obstacle...にすると{}でくくる必要があり、3行に増える) +// (L57をif(!obstacle...にすると{}でくくる必要があり、3行に増える) // が、あんまりよくないだろうか?(良くない気がするが聞いてみたい) // (行数は無駄に増やさない方が読みやすいと思っている) // - 書いている時は気づかなかったが行列(二次元配列)にwidthやheightという単語は @@ -36,7 +36,8 @@ using namespace std; // // // -// L39(行数調整用コメント) +// +// L40(行数調整用コメント) class Solution { public: int uniquePathsWithObstacles(vector>& obstacleGrid) { From 0e88a8fe67af14a991292f5f250d0b2b883698ee Mon Sep 17 00:00:00 2001 From: philip82148 <92205270+philip82148@users.noreply.github.com> Date: Sat, 8 Feb 2025 17:24:25 +0900 Subject: [PATCH 5/6] add 2.cpp --- 31-45/32.Unique-Paths-II/2.cpp | 38 ++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 31-45/32.Unique-Paths-II/2.cpp diff --git a/31-45/32.Unique-Paths-II/2.cpp b/31-45/32.Unique-Paths-II/2.cpp new file mode 100644 index 0000000..6d12a71 --- /dev/null +++ b/31-45/32.Unique-Paths-II/2.cpp @@ -0,0 +1,38 @@ + +#if __has_include("../../debug.hpp") +#include "../../debug.hpp" +#endif +// ここまでローカルでのデバッグ用なので気にしないでください -------------------- + +#include + +using namespace std; + +// <時間> +// 4分 +// <感想> +class Solution { + public: + int uniquePathsWithObstacles(vector>& obstacleGrid) { + if (obstacleGrid[0][0]) return 0; + + int num_rows = obstacleGrid.size(), num_cols = obstacleGrid[0].size(); + vector> num_paths(num_rows, vector(num_cols)); + num_paths[0][0] = 1; + for (int i = 0; i < num_rows; ++i) { + if (obstacleGrid[i][0]) break; + num_paths[i][0] = 1; + } + for (int i = 0; i < num_cols; ++i) { + if (obstacleGrid[0][i]) break; + num_paths[0][i] = 1; + } + for (int i = 1; i < num_rows; ++i) { + for (int j = 1; j < num_cols; ++j) { + if (obstacleGrid[i][j]) continue; + num_paths[i][j] = num_paths[i - 1][j] + num_paths[i][j - 1]; + } + } + return num_paths[num_rows - 1][num_cols - 1]; + } +}; From a55b417c763bda260141aeebb01da38a04794c5a Mon Sep 17 00:00:00 2001 From: philip82148 <92205270+philip82148@users.noreply.github.com> Date: Sat, 8 Feb 2025 17:28:51 +0900 Subject: [PATCH 6/6] remove unnecessary lf --- 31-45/32.Unique-Paths-II/2.cpp | 1 - 1 file changed, 1 deletion(-) diff --git a/31-45/32.Unique-Paths-II/2.cpp b/31-45/32.Unique-Paths-II/2.cpp index 6d12a71..8f99156 100644 --- a/31-45/32.Unique-Paths-II/2.cpp +++ b/31-45/32.Unique-Paths-II/2.cpp @@ -1,4 +1,3 @@ - #if __has_include("../../debug.hpp") #include "../../debug.hpp" #endif