From c5bc3947ea630ae56e749cff0f8d5c6b9ad2a4ca Mon Sep 17 00:00:00 2001 From: Ryo Oshima Date: Mon, 9 Jun 2025 01:20:38 +0900 Subject: [PATCH 1/2] 63. Unique Paths II --- 63. Unique Paths II/solution1_1.java | 40 +++++++++++++++++++ 63. Unique Paths II/solution1_2.java | 47 ++++++++++++++++++++++ 63. Unique Paths II/solution2_1.java | 60 ++++++++++++++++++++++++++++ 3 files changed, 147 insertions(+) create mode 100644 63. Unique Paths II/solution1_1.java create mode 100644 63. Unique Paths II/solution1_2.java create mode 100644 63. Unique Paths II/solution2_1.java diff --git a/63. Unique Paths II/solution1_1.java b/63. Unique Paths II/solution1_1.java new file mode 100644 index 0000000..4eff9da --- /dev/null +++ b/63. Unique Paths II/solution1_1.java @@ -0,0 +1,40 @@ +/* + * ・概要 + * 自力解法 + * 一つ前のUniquePathと同様の方法を思いついた(動的計画法)。違いは障害物があればpathを0にするくらい。 + * 注意点として、スタート地点に障害物があるケースもある。(これで1回とおらなかった。) + * + * ・計算量 + * 時間:O(row * col) + * 空間: O(row * col) + * + * ・その他 + * ループないで障害物みるからl22でとりあえず1いれてるけど逆にわかりにくいかもしれない + * UniquePathと同じように1次元配列でもできるなという思い(ちょっとわかりにくくはなるが) + */ + +public class solution1_1 { + private static final int OBSTACLE = 1; + + public int uniquePathsWithObstacles(int[][] obstacleGrid) { + int rowLimit = obstacleGrid.length; + int colLimit = obstacleGrid[0].length; + int[][] numOfPaths = new int[rowLimit][colLimit]; + numOfPaths[0][0] = 1; + for (int row = 0; row < rowLimit; row++) { + for (int col = 0; col < colLimit; col++) { + if (obstacleGrid[row][col] == OBSTACLE) { + numOfPaths[row][col] = 0; + continue; + } + if (row != 0) { + numOfPaths[row][col] += numOfPaths[row - 1][col]; + } + if (col != 0) { + numOfPaths[row][col] += numOfPaths[row][col - 1]; + } + } + } + return numOfPaths[rowLimit - 1][colLimit - 1]; + } +} diff --git a/63. Unique Paths II/solution1_2.java b/63. Unique Paths II/solution1_2.java new file mode 100644 index 0000000..edc5fb0 --- /dev/null +++ b/63. Unique Paths II/solution1_2.java @@ -0,0 +1,47 @@ +/* + * ・概要 + * 自力解法 + * 練習のため再帰バージョンでも書いてみた。やはりDPのが直感的な気がする。 + * 注意点として、ゴール地点に障害物があるケースもある。(これで1回とおらなかった。) + * + * ・計算量 + * 時間:O(row * col) + * 空間: O(row * col) + */ + +public class solution1_2 { + private static final int OBSTACLE = 1; + + public int uniquePathsWithObstacles(int[][] obstacleGrid) { + if (obstacleGrid[0][0] == 1) { + return 0; + } + int rowLimit = obstacleGrid.length; + int colLimit = obstacleGrid[0].length; + int[][] memo = new int[rowLimit][colLimit]; + for (int row = 0; row < rowLimit; row++) { + Arrays.fill(memo[row], -1); + } + return countPaths(obstacleGrid, 0, 0, rowLimit - 1, colLimit - 1, memo); + } + + private int countPaths( + int[][] obstacleGrid, int row, int col, int goalRow, int goalCol, int[][] memo) { + if (goalRow < row || goalCol < col) { + return 0; + } + if (obstacleGrid[row][col] == OBSTACLE) { + return 0; + } + if (row == goalRow && col == goalCol) { + return 1; + } + if (memo[row][col] != -1) { + return memo[row][col]; + } + memo[row][col] = + countPaths(obstacleGrid, row + 1, col, goalRow, goalCol, memo) + + countPaths(obstacleGrid, row, col + 1, goalRow, goalCol, memo); + return memo[row][col]; + } +} diff --git a/63. Unique Paths II/solution2_1.java b/63. Unique Paths II/solution2_1.java new file mode 100644 index 0000000..7886798 --- /dev/null +++ b/63. Unique Paths II/solution2_1.java @@ -0,0 +1,60 @@ +/* + * https://github.com/irohafternoon/LeetCode/pull/37/files + * numRow→良さそう!! + * + * https://github.com/Fuminiton/LeetCode/pull/34/files + * ネストが少しだけ深いかも(個人的には今回は処理も単純なので許容くらいの感じ)ので、関数化してもいいかも + * + * https://github.com/olsen-blue/Arai60/pull/34/files#r1979987243 + * これは大事。実務ではここらへんはコミュニケーションすぐあ、信用できないので念の為バリデーションをする方針はよくとられがちなイメージ(僕の経験上) + * + * https://github.com/olsen-blue/Arai60/pull/34/files#r1979998266 + * たしかに、row,colくらいならr,cでも悪くないかなという気がしてきた。 + * https://google.github.io/styleguide/javaguide.html#s4.8.2-variable-declarations + * ガイドをみてもループないならOKそう + * 入力配列のバリデーションもやはり意識すべし + * エッジ初期化を先にする手法。コード量は増えるがやってることは少し、明示的な気もするなくらいな気持ち。 + * + */ +public class solution2_1 { + static int OBSTACLE = 1; + + public int uniquePathsWithObstacles(int[][] obstacleGrid) { + int numOfRow = obstacleGrid.length; + if (numOfRow == 0) { + return 0; + } + int numOfCol = obstacleGrid[0].length; + if (numOfCol == 0) { + return 0; + } + + int[][] numOfPaths = new int[numOfRow][numOfCol]; + for (int c = 0; c < numOfCol; c++) { + if (obstacleGrid[0][c] != OBSTACLE) { + numOfPaths[0][c] = 1; + } + } + for (int r = 0; r < numOfRow; r++) { + if (obstacleGrid[r][0] != OBSTACLE) { + numOfPaths[r][0] = 1; + } + } + + for (int r = 1; r < numOfRow; r++) { + for (int c = 1; c < numOfCol; c++) { + if (obstacleGrid[r][c] == OBSTACLE) { + numOfPaths[r][c] = 0; + continue; + } + if (r != 0) { + numOfPaths[r][c] += numOfPaths[r - 1][c]; + } + if (c != 0) { + numOfPaths[r][c] += numOfPaths[r][c - 1]; + } + } + } + return numOfPaths[numOfRow - 1][numOfCol - 1]; + } +} From d93c2c1673714ff435b1b802d1b293a1910ed43c Mon Sep 17 00:00:00 2001 From: Ryo Oshima Date: Mon, 9 Jun 2025 01:27:03 +0900 Subject: [PATCH 2/2] =?UTF-8?q?solution2=5F1=E4=BF=AE=E6=AD=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 63. Unique Paths II/solution2_1.java | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/63. Unique Paths II/solution2_1.java b/63. Unique Paths II/solution2_1.java index 7886798..509d77c 100644 --- a/63. Unique Paths II/solution2_1.java +++ b/63. Unique Paths II/solution2_1.java @@ -28,15 +28,24 @@ public int uniquePathsWithObstacles(int[][] obstacleGrid) { if (numOfCol == 0) { return 0; } - + if (obstacleGrid[0][0] == OBSTACLE || obstacleGrid[numOfRow - 1][numOfCol - 1] == OBSTACLE) { + return 0; + } int[][] numOfPaths = new int[numOfRow][numOfCol]; + numOfPaths[0][0] = 1; for (int c = 0; c < numOfCol; c++) { - if (obstacleGrid[0][c] != OBSTACLE) { + if (obstacleGrid[0][c] == OBSTACLE) { + numOfPaths[0][c] = 0; + break; + } else { numOfPaths[0][c] = 1; } } for (int r = 0; r < numOfRow; r++) { - if (obstacleGrid[r][0] != OBSTACLE) { + if (obstacleGrid[r][0] == OBSTACLE) { + numOfPaths[r][0] = 0; + break; + } else { numOfPaths[r][0] = 1; } }