From 71c57a9ed698cc9c972ca8b9f99e16521de57163 Mon Sep 17 00:00:00 2001 From: eatonjiang Date: Wed, 23 Dec 2020 16:49:54 +0800 Subject: [PATCH 1/4] add MinimumPathSum DynamicProgramming --- DynamicProgramming/MinimumPathSum.java | 63 ++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) create mode 100644 DynamicProgramming/MinimumPathSum.java diff --git a/DynamicProgramming/MinimumPathSum.java b/DynamicProgramming/MinimumPathSum.java new file mode 100644 index 000000000000..3c1c224994c7 --- /dev/null +++ b/DynamicProgramming/MinimumPathSum.java @@ -0,0 +1,63 @@ +package DynamicProgramming; + +/* + Given the following grid with length m and width n: + \---\---\---\ (n) + \ 1 \ 3 \ 1 \ + \---\---\---\ + \ 1 \ 5 \ 1 \ + \---\---\---\ + \ 4 \ 2 \ 1 \ + \---\---\---\ + (m) + Find the path where its sum is the smallest. + The Time Complexity of your algorithm should be smaller than or equal to O(mn). + The Space Complexity of your algorithm should be smaller than or equal to O(mn). + You can only move from the top left corner to the down right corner. + You can only move one step down or right. + + EXAMPLE: + INPUT: grid = [[1,3,1],[1,5,1],[4,2,1]] + OUTPUT: 7 + EXPLANATIONS: 1 + 3 + 1 + 1 + 1 = 7 + */ +public class MinimumPathSum { + public static void main(String[] args) { + int[][] grid = { + {1, 3, 1}, + {1, 5, 1}, + {4, 2, 1} + }; +// int[][] grid2 = { +// {1, 2}, +// {5, 6}, +// {1, 1} +// }; +// int [][] grid3 = { +// {2, 3, 3}, +// {7, 2, 1} +// }; + System.out.println(minimumPathSum(grid)); + } + + public static int minimumPathSum(int[][] grid) { + int m = grid.length, n = grid[0].length; + if (n == 0) { + return 0; + } + int[][] dp = new int[m][n]; + dp[0][0] = grid[0][0]; + for (int i = 0; i < n - 1; i ++) { + dp[0][i + 1] = dp[0][i] + grid[0][i + 1]; + } + for (int i = 0; i < m - 1; i ++) { + dp[i + 1][0] = dp[i][0] + grid[i + 1][0]; + } + for (int i = 1; i < m; i ++) { + for (int j = 1; j < n; j ++) { + dp[i][j] = Math.min(dp[i - 1][j], dp[i][j - 1]) + grid[i][j]; + } + } + return dp[m - 1][n - 1]; + } +} From 9091c605bd0b77515796308da3c6025fad80cddc Mon Sep 17 00:00:00 2001 From: eatonjiang Date: Wed, 23 Dec 2020 17:31:37 +0800 Subject: [PATCH 2/4] add tests and link for the algorithm --- DynamicProgramming/MinimumPathSum.java | 44 ++++++++++++++++++++------ 1 file changed, 34 insertions(+), 10 deletions(-) diff --git a/DynamicProgramming/MinimumPathSum.java b/DynamicProgramming/MinimumPathSum.java index 3c1c224994c7..cc35cd36a3bd 100644 --- a/DynamicProgramming/MinimumPathSum.java +++ b/DynamicProgramming/MinimumPathSum.java @@ -1,5 +1,7 @@ package DynamicProgramming; +import org.junit.Test; + /* Given the following grid with length m and width n: \---\---\---\ (n) @@ -11,6 +13,8 @@ \---\---\---\ (m) Find the path where its sum is the smallest. + + All numbers given are positive. The Time Complexity of your algorithm should be smaller than or equal to O(mn). The Space Complexity of your algorithm should be smaller than or equal to O(mn). You can only move from the top left corner to the down right corner. @@ -20,23 +24,43 @@ The Space Complexity of your algorithm should be smaller than or equal to O(mn). INPUT: grid = [[1,3,1],[1,5,1],[4,2,1]] OUTPUT: 7 EXPLANATIONS: 1 + 3 + 1 + 1 + 1 = 7 + + For more information see https://www.geeksforgeeks.org/maximum-path-sum-matrix/ */ public class MinimumPathSum { - public static void main(String[] args) { + + @Test + public void testRegular() { int[][] grid = { {1, 3, 1}, {1, 5, 1}, {4, 2, 1} }; -// int[][] grid2 = { -// {1, 2}, -// {5, 6}, -// {1, 1} -// }; -// int [][] grid3 = { -// {2, 3, 3}, -// {7, 2, 1} -// }; + System.out.println(minimumPathSum(grid)); + } + + @Test + public void testLessColumns() { + int[][] grid = { + {1, 2}, + {5, 6}, + {1, 1} + }; + System.out.println(minimumPathSum(grid)); + } + + @Test + public void testLessRows() { + int [][] grid = { + {2, 3, 3}, + {7, 2, 1} + }; + System.out.println(minimumPathSum(grid)); + } + + @Test + public void testOneRowOneColumn() { + int[][] grid = {{2}}; System.out.println(minimumPathSum(grid)); } From 8230ce597bd11ce3249df86ae65ddd8811c73bcc Mon Sep 17 00:00:00 2001 From: eatonjiang Date: Wed, 23 Dec 2020 17:46:24 +0800 Subject: [PATCH 3/4] remove junit dependency --- DynamicProgramming/MinimumPathSum.java | 6 ------ 1 file changed, 6 deletions(-) diff --git a/DynamicProgramming/MinimumPathSum.java b/DynamicProgramming/MinimumPathSum.java index cc35cd36a3bd..069b8c011475 100644 --- a/DynamicProgramming/MinimumPathSum.java +++ b/DynamicProgramming/MinimumPathSum.java @@ -1,7 +1,5 @@ package DynamicProgramming; -import org.junit.Test; - /* Given the following grid with length m and width n: \---\---\---\ (n) @@ -29,7 +27,6 @@ The Space Complexity of your algorithm should be smaller than or equal to O(mn). */ public class MinimumPathSum { - @Test public void testRegular() { int[][] grid = { {1, 3, 1}, @@ -39,7 +36,6 @@ public void testRegular() { System.out.println(minimumPathSum(grid)); } - @Test public void testLessColumns() { int[][] grid = { {1, 2}, @@ -49,7 +45,6 @@ public void testLessColumns() { System.out.println(minimumPathSum(grid)); } - @Test public void testLessRows() { int [][] grid = { {2, 3, 3}, @@ -58,7 +53,6 @@ public void testLessRows() { System.out.println(minimumPathSum(grid)); } - @Test public void testOneRowOneColumn() { int[][] grid = {{2}}; System.out.println(minimumPathSum(grid)); From c3a3e13b1265771e9b646d31efe70367c7c79fff Mon Sep 17 00:00:00 2001 From: eatonjiang Date: Wed, 23 Dec 2020 18:06:34 +0800 Subject: [PATCH 4/4] format with google code format --- DynamicProgramming/MinimumPathSum.java | 134 ++++++++++++------------- 1 file changed, 67 insertions(+), 67 deletions(-) diff --git a/DynamicProgramming/MinimumPathSum.java b/DynamicProgramming/MinimumPathSum.java index 069b8c011475..cc0fcaa4ed98 100644 --- a/DynamicProgramming/MinimumPathSum.java +++ b/DynamicProgramming/MinimumPathSum.java @@ -1,81 +1,81 @@ package DynamicProgramming; /* - Given the following grid with length m and width n: - \---\---\---\ (n) - \ 1 \ 3 \ 1 \ - \---\---\---\ - \ 1 \ 5 \ 1 \ - \---\---\---\ - \ 4 \ 2 \ 1 \ - \---\---\---\ - (m) - Find the path where its sum is the smallest. +Given the following grid with length m and width n: +\---\---\---\ (n) +\ 1 \ 3 \ 1 \ +\---\---\---\ +\ 1 \ 5 \ 1 \ +\---\---\---\ +\ 4 \ 2 \ 1 \ +\---\---\---\ +(m) +Find the path where its sum is the smallest. - All numbers given are positive. - The Time Complexity of your algorithm should be smaller than or equal to O(mn). - The Space Complexity of your algorithm should be smaller than or equal to O(mn). - You can only move from the top left corner to the down right corner. - You can only move one step down or right. +All numbers given are positive. +The Time Complexity of your algorithm should be smaller than or equal to O(mn). +The Space Complexity of your algorithm should be smaller than or equal to O(mn). +You can only move from the top left corner to the down right corner. +You can only move one step down or right. - EXAMPLE: - INPUT: grid = [[1,3,1],[1,5,1],[4,2,1]] - OUTPUT: 7 - EXPLANATIONS: 1 + 3 + 1 + 1 + 1 = 7 +EXAMPLE: +INPUT: grid = [[1,3,1],[1,5,1],[4,2,1]] +OUTPUT: 7 +EXPLANATIONS: 1 + 3 + 1 + 1 + 1 = 7 - For more information see https://www.geeksforgeeks.org/maximum-path-sum-matrix/ - */ +For more information see https://www.geeksforgeeks.org/maximum-path-sum-matrix/ +*/ public class MinimumPathSum { - public void testRegular() { - int[][] grid = { - {1, 3, 1}, - {1, 5, 1}, - {4, 2, 1} - }; - System.out.println(minimumPathSum(grid)); - } + public void testRegular() { + int[][] grid = { + {1, 3, 1}, + {1, 5, 1}, + {4, 2, 1} + }; + System.out.println(minimumPathSum(grid)); + } - public void testLessColumns() { - int[][] grid = { - {1, 2}, - {5, 6}, - {1, 1} - }; - System.out.println(minimumPathSum(grid)); - } + public void testLessColumns() { + int[][] grid = { + {1, 2}, + {5, 6}, + {1, 1} + }; + System.out.println(minimumPathSum(grid)); + } - public void testLessRows() { - int [][] grid = { - {2, 3, 3}, - {7, 2, 1} - }; - System.out.println(minimumPathSum(grid)); - } + public void testLessRows() { + int[][] grid = { + {2, 3, 3}, + {7, 2, 1} + }; + System.out.println(minimumPathSum(grid)); + } - public void testOneRowOneColumn() { - int[][] grid = {{2}}; - System.out.println(minimumPathSum(grid)); - } + public void testOneRowOneColumn() { + int[][] grid = {{2}}; + System.out.println(minimumPathSum(grid)); + } - public static int minimumPathSum(int[][] grid) { - int m = grid.length, n = grid[0].length; - if (n == 0) { - return 0; - } - int[][] dp = new int[m][n]; - dp[0][0] = grid[0][0]; - for (int i = 0; i < n - 1; i ++) { - dp[0][i + 1] = dp[0][i] + grid[0][i + 1]; - } - for (int i = 0; i < m - 1; i ++) { - dp[i + 1][0] = dp[i][0] + grid[i + 1][0]; - } - for (int i = 1; i < m; i ++) { - for (int j = 1; j < n; j ++) { - dp[i][j] = Math.min(dp[i - 1][j], dp[i][j - 1]) + grid[i][j]; - } - } - return dp[m - 1][n - 1]; + public static int minimumPathSum(int[][] grid) { + int m = grid.length, n = grid[0].length; + if (n == 0) { + return 0; + } + int[][] dp = new int[m][n]; + dp[0][0] = grid[0][0]; + for (int i = 0; i < n - 1; i++) { + dp[0][i + 1] = dp[0][i] + grid[0][i + 1]; + } + for (int i = 0; i < m - 1; i++) { + dp[i + 1][0] = dp[i][0] + grid[i + 1][0]; + } + for (int i = 1; i < m; i++) { + for (int j = 1; j < n; j++) { + dp[i][j] = Math.min(dp[i - 1][j], dp[i][j - 1]) + grid[i][j]; + } } + return dp[m - 1][n - 1]; + } }