From 2a7f3c4f6bf57a696498cc74ab3ea7c732bea253 Mon Sep 17 00:00:00 2001 From: yennanliu Date: Mon, 4 Mar 2024 22:29:14 +0800 Subject: [PATCH] add 91 java, update progress --- README.md | 2 +- data/progress.txt | 2 +- data/to_review.txt | 18 +++--- .../DynamicProgramming/DecodeWays.java | 62 +++++++++++++++++++ 4 files changed, 73 insertions(+), 11 deletions(-) create mode 100644 leetcode_java/src/main/java/LeetCodeJava/DynamicProgramming/DecodeWays.java diff --git a/README.md b/README.md index 7408df36..36b613c0 100644 --- a/README.md +++ b/README.md @@ -1015,7 +1015,7 @@ 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), [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) +091| [Decode Ways](https://leetcode.com/problems/decode-ways/) | [Python](./leetcode_python/Dynamic_Programming/decode-ways.py), [Java](./leetcode_java/src/main/java/LeetCodeJava/DynamicProgramming/DecodeWays.java) | _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) 120| [Triangle](https://leetcode.com/problems/triangle/) | [Python](./leetcode_python/Dynamic_Programming/triangle.py) | _O(m * n)_ | _O(n)_ | Medium |`amazon`| AGAIN* (not start) (2) 123| [Best Time to Buy and Sell Stock III](https://leetcode.com/problems/best-time-to-buy-and-sell-stock-iii/)| [Python](./leetcode_python/Dynamic_Programming/best-time-to-buy-and-sell-stock-iii.py) | _O(m * n)_ | _O(n)_ | Hard |`amazon`, uber, apple| AGAIN* (not start) (1) diff --git a/data/progress.txt b/data/progress.txt index e495035e..57dcb75d 100644 --- a/data/progress.txt +++ b/data/progress.txt @@ -1,4 +1,4 @@ -20240304: 73,76,79(again) +20240304: 73,76,79(again),91 20240303: 55(again),56,62,70 20240229: 39,48(again),49,53,54 20240228: 20,21,23,33(again) diff --git a/data/to_review.txt b/data/to_review.txt index 6693280e..4d5e1575 100644 --- a/data/to_review.txt +++ b/data/to_review.txt @@ -1,28 +1,28 @@ -2024-04-28 -> ['73,76,79(again)'] +2024-04-28 -> ['73,76,79(again),91'] 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-07 -> ['73,76,79(again)'] +2024-04-07 -> ['73,76,79(again),91'] 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-25 -> ['73,76,79(again)'] +2024-03-25 -> ['73,76,79(again),91'] 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-17 -> ['73,76,79(again)'] +2024-03-17 -> ['73,76,79(again),91'] 2024-03-16 -> ['55(again),56,62,70'] 2024-03-13 -> ['39,48(again),49,53,54'] -2024-03-12 -> ['73,76,79(again)', '20,21,23,33(again)'] +2024-03-12 -> ['73,76,79(again),91', '20,21,23,33(again)'] 2024-03-11 -> ['55(again),56,62,70', '1,3,5,4,19'] -2024-03-09 -> ['73,76,79(again)'] +2024-03-09 -> ['73,76,79(again),91'] 2024-03-08 -> ['55(again),56,62,70', '39,48(again),49,53,54'] -2024-03-07 -> ['73,76,79(again)', '20,21,23,33(again)'] -2024-03-06 -> ['73,76,79(again)', '55(again),56,62,70', '1,3,5,4,19'] -2024-03-05 -> ['73,76,79(again)', '55(again),56,62,70', '39,48(again),49,53,54'] +2024-03-07 -> ['73,76,79(again),91', '20,21,23,33(again)'] +2024-03-06 -> ['73,76,79(again),91', '55(again),56,62,70', '1,3,5,4,19'] +2024-03-05 -> ['73,76,79(again),91', '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)'] diff --git a/leetcode_java/src/main/java/LeetCodeJava/DynamicProgramming/DecodeWays.java b/leetcode_java/src/main/java/LeetCodeJava/DynamicProgramming/DecodeWays.java new file mode 100644 index 00000000..2b803e85 --- /dev/null +++ b/leetcode_java/src/main/java/LeetCodeJava/DynamicProgramming/DecodeWays.java @@ -0,0 +1,62 @@ +package LeetCodeJava.DynamicProgramming; + +// https://leetcode.com/problems/decode-ways/description/ + +public class DecodeWays { + + // V0 + // IDEA : DP + // TODO : implement + + // V1 + // IDEA : DP + // https://leetcode.com/problems/decode-ways/solutions/4456554/character-state-machine-video-solution-java-c/ + public int numDecodings(String encodedString) { + char s[] = encodedString.toCharArray(); + if(s[0] == '0')return 0; + int sz = s.length; + int noWaysAtIndx[] = new int[sz]; + noWaysAtIndx[0] = 1; + for(int indx = 1; indx < sz; indx++){ + char currC = s[indx], prevC = s[indx-1]; + if(currC == '0' && !(prevC == '1' || prevC == '2')){ + return 0; + } + int onesDigit = currC - '0', tensDigit = prevC - '0'; + int number = tensDigit * 10 + onesDigit; + if(number >= 10 && number <= 26){ + if(indx >= 2) + noWaysAtIndx[indx] += noWaysAtIndx[indx-2]; + else + noWaysAtIndx[indx] = 1; + } + if(number != 10 && number != 20){ + noWaysAtIndx[indx] += noWaysAtIndx[indx-1]; + } + } + return noWaysAtIndx[sz - 1]; + } + + // V2 + // IDEA : DP + // https://leetcode.com/problems/decode-ways/solutions/4454539/decode-ways-java/ + public int numDecodings_1(String s) { + int n = s.length(); + int[] dp = new int[n + 1]; + if(n == 0 || s.charAt(0) == '0') return 0; + dp[0] = 1; dp[1] = 1; + + for(int i = 2; i <= n; i++){ + // check for single-digit + if(s.charAt(i - 1) >= '1' && s.charAt(i - 1) <= '9') dp[i] = dp[i - 1]; + + // check for two-digits + if(s.charAt(i - 2) == '1') dp[i] += dp[i - 2]; + else if(s.charAt(i - 2) == '2' && s.charAt(i - 1) >= '0' && s.charAt(i - 1) <= '6') dp[i] += dp[i - 2]; + } + return dp[n]; + } + + + +}