diff --git a/README.md b/README.md index 446b3cbf..3e88fa94 100644 --- a/README.md +++ b/README.md @@ -1013,7 +1013,7 @@ 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) +070| [Climbing Stairs](https://leetcode.com/problems/climbing-stairs/)| [Python](./leetcode_python/Dynamic_Programming/climbing-stairs.py), [Java](./leetcode_java/src/main/java/LeetCodeJava/DynamicProgramming/ClimbingStairs.java)| _O(logn)_ | _O(1)_| Easy| Curated Top 75, Matrix Exponentiation, `DP`, `recursion`, `apple`| OK* (2) 072| [Edit Distance](https://leetcode.com/problems/edit-distance/)| [Python](./leetcode_python/Dynamic_Programming/edit-distance.py) | _O(logn)_ | _O(1)_| Hard| top 100 like, `DP`, `recursion`, `apple`, google, amazon| again (not start) 091| [Decode Ways](https://leetcode.com/problems/decode-ways/) | [Python](./leetcode_python/Dynamic_Programming/decode-ways.py) | _O(n)_ | _O(1)_ | Medium |Curated Top 75, `good basic`,`dp`, `M$`, `UBER`, `amazon`,`fb`| AGAIN************ (4) 096| [Unique Binary Search Trees](https://leetcode.com/problems/unique-binary-search-trees/) | [Python](./leetcode_python/Dynamic_Programming/unique-binary-search-trees.py) | _O(n)_| _O(1)_| Medium| dp, Math, fb, amazon| AGAIN* (2) (not start) diff --git a/data/progress.txt b/data/progress.txt index c6d804fc..9fe11586 100644 --- a/data/progress.txt +++ b/data/progress.txt @@ -1,4 +1,4 @@ -20240303: 55(again),56,62 +20240303: 55(again),56,62,70 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 cc5f6972..285ca26a 100644 --- a/data/to_review.txt +++ b/data/to_review.txt @@ -1,24 +1,24 @@ -2024-04-27 -> ['55(again),56,62'] +2024-04-27 -> ['55(again),56,62,70'] 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,62'] +2024-04-06 -> ['55(again),56,62,70'] 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,62'] +2024-03-24 -> ['55(again),56,62,70'] 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,62'] +2024-03-16 -> ['55(again),56,62,70'] 2024-03-13 -> ['39,48(again),49,53,54'] 2024-03-12 -> ['20,21,23,33(again)'] -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-11 -> ['55(again),56,62,70', '1,3,5,4,19'] +2024-03-08 -> ['55(again),56,62,70', '39,48(again),49,53,54'] 2024-03-07 -> ['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-06 -> ['55(again),56,62,70', '1,3,5,4,19'] +2024-03-05 -> ['55(again),56,62,70', '39,48(again),49,53,54'] +2024-03-04 -> ['55(again),56,62,70', '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/ClimbingStairs.java b/leetcode_java/src/main/java/LeetCodeJava/DynamicProgramming/ClimbingStairs.java new file mode 100644 index 00000000..7ba8a735 --- /dev/null +++ b/leetcode_java/src/main/java/LeetCodeJava/DynamicProgramming/ClimbingStairs.java @@ -0,0 +1,88 @@ +package LeetCodeJava.DynamicProgramming; + +// https://leetcode.com/problems/climbing-stairs/ + +import java.util.*; + +public class ClimbingStairs { + + // V0 + // IDEA : DP + public int climbStairs(int n) { + + // null check + if (n <= 0){ + return 0; + } + + if (n == 1 || n == 2){ + return n; + } + + // NOTE !!! init dp as ArrayList + List dp = new ArrayList<>(); + dp.add(1); // i = 0 + dp.add(1); // i = 1 + //System.out.println("dp = " + dp); + + // NOTE !!! start from i = 2 + for (int i = 2; i <= n; i++){ + int tmp = dp.get(i-1) + dp.get(i-2); + dp.add(tmp); + } + + return dp.get(n); + } + + // V1 + // IDEA : Memoization + // https://leetcode.com/problems/climbing-stairs/solutions/3708750/4-method-s-beat-s-100-c-java-python-beginner-friendly/ + public int climbStairs_2(int n) { + Map memo = new HashMap<>(); + return climbStairs(n, memo); + } + + private int climbStairs(int n, Map memo) { + if (n == 0 || n == 1) { + return 1; + } + if (!memo.containsKey(n)) { + memo.put(n, climbStairs(n-1, memo) + climbStairs(n-2, memo)); + } + return memo.get(n); + } + + // V2 + // IDEA : Tabulation + // https://leetcode.com/problems/climbing-stairs/solutions/3708750/4-method-s-beat-s-100-c-java-python-beginner-friendly/ + public int climbStairs_4(int n) { + if (n == 0 || n == 1) { + return 1; + } + + int[] dp = new int[n+1]; + dp[0] = dp[1] = 1; + + for (int i = 2; i <= n; i++) { + dp[i] = dp[i-1] + dp[i-2]; + } + return dp[n]; + } + + // V3 + // IDEA : Space Optimization + // https://leetcode.com/problems/climbing-stairs/solutions/3708750/4-method-s-beat-s-100-c-java-python-beginner-friendly/ + public int climbStairs_5(int n) { + if (n == 0 || n == 1) { + return 1; + } + int prev = 1, curr = 1; + for (int i = 2; i <= n; i++) { + int temp = curr; + curr = prev + curr; + prev = temp; + } + return curr; + } + +}