From ff263e6d1725a4b8fb7f5ef0d0cb471830336853 Mon Sep 17 00:00:00 2001 From: yennanliu Date: Sun, 3 Mar 2024 18:41:21 +0800 Subject: [PATCH] add 62 java, update progress --- README.md | 2 +- data/progress.txt | 2 +- data/to_review.txt | 18 +-- .../DynamicProgramming/UniquePaths.java | 134 ++++++++++++++++++ 4 files changed, 145 insertions(+), 11 deletions(-) create mode 100644 leetcode_java/src/main/java/LeetCodeJava/DynamicProgramming/UniquePaths.java diff --git a/README.md b/README.md index 522546bd..446b3cbf 100644 --- a/README.md +++ b/README.md @@ -1010,7 +1010,7 @@ | # | Title | Solution | Time | Space | Difficulty | Tag | Status| |-----|---------------- | --------------- | --------------- | --------------- | ------------- |--------------|-----| 053| [Maximum Subarray](https://leetcode.com/problems/maximum-subarray/)| [Python](./leetcode_python/Dynamic_Programming/maximum-subarray.py), [Java](./leetcode_java/src/main/java/LeetCodeJava/Greedy/MaximumSubarray.java) | _O(n)_ | _O(1)_ | Medium |Curated Top 75, brute force, divied & conquer, `DP`,`dp basic`,`amazon`,`fb`| AGAIN******** (5) -062| [Unique Paths](https://leetcode.com/problems/unique-paths/) | [Python](./leetcode_python/Dynamic_Programming/unique-paths.py) | _O(m * n)_| _O(m + n)_ | Medium|Curated Top 75, `dp`, `basic`, fb, google, apple, amazon, m$| AGAIN**** (2) +062| [Unique Paths](https://leetcode.com/problems/unique-paths/)| [Python](./leetcode_python/Dynamic_Programming/unique-paths.py), [Java](./leetcode_java/src/main/java/LeetCodeJava/DynamicProgramming/UniquePaths.java) | _O(m * n)_| _O(m + n)_ | Medium|Curated Top 75, `dp`, `basic`, fb, google, apple, amazon, m$| AGAIN**** (2) 063| [Unique Paths II](https://leetcode.com/problems/unique-paths-ii/) | [Python](./leetcode_python/Dynamic_Programming/unique-paths-ii.py) | _O(m * n)_ | _O(m + n)_ | Medium |`trick`, check `#062 Unique Paths`, `amazon`| AGAIN* 064| [Minimum Path Sum](https://leetcode.com/problems/minimum-path-sum/)| [Python](./leetcode_python/Dynamic_Programming/minimum-path-sum.py) | _O(m * n)_ | _O(m + n)_ | Medium |`DP`, `amazon`| AGAIN* 070| [Climbing Stairs](https://leetcode.com/problems/climbing-stairs/)| [Python](./leetcode_python/Dynamic_Programming/climbing-stairs.py) | _O(logn)_ | _O(1)_| Easy| Curated Top 75, Matrix Exponentiation, `DP`, `recursion`, `apple`| OK* (2) diff --git a/data/progress.txt b/data/progress.txt index f7182ce2..c6d804fc 100644 --- a/data/progress.txt +++ b/data/progress.txt @@ -1,4 +1,4 @@ -20240303: 55(again),56 +20240303: 55(again),56,62 20240229: 39,48(again),49,53,54 20240228: 20,21,23,33(again) 20240227: 1,3,5,4,19 diff --git a/data/to_review.txt b/data/to_review.txt index 1de1f7b1..cc5f6972 100644 --- a/data/to_review.txt +++ b/data/to_review.txt @@ -1,24 +1,24 @@ -2024-04-27 -> ['55(again),56'] +2024-04-27 -> ['55(again),56,62'] 2024-04-24 -> ['39,48(again),49,53,54'] 2024-04-23 -> ['20,21,23,33(again)'] 2024-04-22 -> ['1,3,5,4,19'] -2024-04-06 -> ['55(again),56'] +2024-04-06 -> ['55(again),56,62'] 2024-04-03 -> ['39,48(again),49,53,54'] 2024-04-02 -> ['20,21,23,33(again)'] 2024-04-01 -> ['1,3,5,4,19'] -2024-03-24 -> ['55(again),56'] +2024-03-24 -> ['55(again),56,62'] 2024-03-21 -> ['39,48(again),49,53,54'] 2024-03-20 -> ['20,21,23,33(again)'] 2024-03-19 -> ['1,3,5,4,19'] -2024-03-16 -> ['55(again),56'] +2024-03-16 -> ['55(again),56,62'] 2024-03-13 -> ['39,48(again),49,53,54'] 2024-03-12 -> ['20,21,23,33(again)'] -2024-03-11 -> ['55(again),56', '1,3,5,4,19'] -2024-03-08 -> ['55(again),56', '39,48(again),49,53,54'] +2024-03-11 -> ['55(again),56,62', '1,3,5,4,19'] +2024-03-08 -> ['55(again),56,62', '39,48(again),49,53,54'] 2024-03-07 -> ['20,21,23,33(again)'] -2024-03-06 -> ['55(again),56', '1,3,5,4,19'] -2024-03-05 -> ['55(again),56', '39,48(again),49,53,54'] -2024-03-04 -> ['55(again),56', '20,21,23,33(again)'] +2024-03-06 -> ['55(again),56,62', '1,3,5,4,19'] +2024-03-05 -> ['55(again),56,62', '39,48(again),49,53,54'] +2024-03-04 -> ['55(again),56,62', '20,21,23,33(again)'] 2024-03-03 -> ['39,48(again),49,53,54', '1,3,5,4,19'] 2024-03-02 -> ['39,48(again),49,53,54', '20,21,23,33(again)'] 2024-03-01 -> ['39,48(again),49,53,54', '20,21,23,33(again)', '1,3,5,4,19'] diff --git a/leetcode_java/src/main/java/LeetCodeJava/DynamicProgramming/UniquePaths.java b/leetcode_java/src/main/java/LeetCodeJava/DynamicProgramming/UniquePaths.java new file mode 100644 index 00000000..2b34eed3 --- /dev/null +++ b/leetcode_java/src/main/java/LeetCodeJava/DynamicProgramming/UniquePaths.java @@ -0,0 +1,134 @@ +package LeetCodeJava.DynamicProgramming; + +// https://leetcode.com/problems/unique-paths/description/ + + +import java.math.BigInteger; + +public class UniquePaths { + + // VO + // IDEA : MATH + // -> the UNIQUE combination of x "a", and y "b" + // -> e.g. [a, a,....a] and [b,b...,,,,b] + // <- x count -> <- y count -> + // -> so the combination count is + // (x+y)! / (x! * y!) + public int uniquePaths(int m, int n) { + if (m == 0 || n == 0) { + return 0; + } + if (m == 1 || n == 1) { + return 1; + } + + /** NOTE !!! BigInteger op code */ + BigInteger res = getFactorial((m - 1) + (n - 1)) + .divide(getFactorial(m - 1).multiply(getFactorial(n - 1))); + return res.intValue(); + } + + private BigInteger getFactorial(int x) { + if (x <= 0) { + throw new ArithmeticException("x should be equal or bigger than 1"); + } + /** NOTE !!! BigInteger op code */ + BigInteger res = BigInteger.ONE; + for (int i = 1; i < x + 1; i++) { + res = res.multiply(BigInteger.valueOf(i)); + } + return res; + } + + // V1 + // https://leetcode.com/problems/unique-paths/solutions/4795217/memoization-and-tabulation-java-100-beats/ + public int uniquePaths_1(int m, int n) { + Integer[][] memo = new Integer[m][n]; + return findPath(m - 1, n - 1, memo); + } + + private int findPath(int r, int c, Integer[][] memo){ + + if(r == 0 && c == 0) + return 1; + + if(r < 0 || c < 0) + return 0; + + if(memo[r][c] != null) + return memo[r][c]; + + int up = findPath(r - 1, c, memo); + int left = findPath(r, c - 1, memo); + + memo[r][c] = up + left; + + return memo[r][c]; + } + + // V2 + // https://leetcode.com/problems/unique-paths/solutions/4801294/recurrsion-memoization-tabulation-easy-explaination/ + // Recurrsion + // This is a reccursive solution and will give TLE with reccursive solution, try these with DP + // public int uniquePaths(int m, int n) { + // return f(m, n, m-1, n-1); + // } + + // public int f(int m, int n, int r, int c){ + // if(r == || c == n-1){ + // return 1; + // } + // if(r < 0 || c < 0){ + // return 0; + // } + + // int up= f(m, n, r-1, c); + // int left= f(m, n, r, c-1); + // return up + left; + // } + + + //Memoization + // public int uniquePaths(int m, int n) { + // int[][] dp= new int[m+1][n+1]; + // for (int[] row : dp) { + // Arrays.fill(row, -1); // Initialize each cell of the array individually + // } + // return f(m, n, m-1, n-1, dp); + // } + + // public int f(int m, int n, int r, int c, int[][] dp){ + // if(r == 0 || c == 0){ + // return 1; + // } + // if(r < 0 || c < 0){ + // return 0; + // } + + // if(dp[r][c] != -1){ + // return dp[r][c]; + // } + // int up= f(m, n, r-1, c, dp); + // int left= f(m, n, r, c-1, dp); + // return dp[r][c]= up + left; + // } + + // Tabulation + public int uniquePaths_2(int m, int n) { + int[][] dp= new int[m+1][n+1]; + dp[0][0]= 1; + + for(int i=0; i< m; i++){ + for(int j=0; j< n; j++){ + if(i==0 && j==0) continue; + int up= 0; + int left=0; + if(i>0) up= dp[i-1][j]; + if(j>0) left= dp[i][j-1]; + dp[i][j]= up + left; + } + } + return dp[m-1][n-1]; + } + +}