From 7dfed679722a27a93de7f9c1d730ba4f2b504e4a Mon Sep 17 00:00:00 2001 From: bky373 Date: Thu, 18 Jul 2024 16:28:11 +0900 Subject: [PATCH 1/2] Add Week 11 Solutions --- coin-change/bky373.java | 20 ++++++++++++++++++ decode-ways/bky373.java | 22 ++++++++++++++++++++ maximum-product-subarray/bky373.java | 29 ++++++++++++++++++++++++++ palindromic-substrings/bky373.java | 25 ++++++++++++++++++++++ word-break/bky373.java | 31 ++++++++++++++++++++++++++++ 5 files changed, 127 insertions(+) create mode 100644 coin-change/bky373.java create mode 100644 decode-ways/bky373.java create mode 100644 maximum-product-subarray/bky373.java create mode 100644 palindromic-substrings/bky373.java create mode 100644 word-break/bky373.java diff --git a/coin-change/bky373.java b/coin-change/bky373.java new file mode 100644 index 000000000..c09984afb --- /dev/null +++ b/coin-change/bky373.java @@ -0,0 +1,20 @@ +class Solution { + + public int coinChange(int[] coins, int amount) { + int max = amount + 1; + int[] dp = new int[amount + 1]; + Arrays.fill(dp, max); + dp[0] = 0; + for (int i = 1; i <= amount; i++) { + for (int j = 0; j < coins.length; j++) { + if (coins[j] <= i) { + dp[i] = Math.min(dp[i], dp[i - coins[j]] + 1); + } + } + } + if (dp[amount] > amount) { + return -1; + } + return dp[amount]; + } +} diff --git a/decode-ways/bky373.java b/decode-ways/bky373.java new file mode 100644 index 000000000..33226dac7 --- /dev/null +++ b/decode-ways/bky373.java @@ -0,0 +1,22 @@ +// time: O(N) +// space: O(N) +class Solution { + + public int numDecodings(String s) { + int[] dp = new int[s.length() + 1]; + dp[0] = 1; + dp[1] = s.charAt(0) == '0' ? 0 : 1; + + for (int i = 2; i < dp.length; i++) { + if (s.charAt(i - 1) != '0') { + dp[i] = dp[i - 1]; + } + + int twoDigits = Integer.valueOf(s.substring(i - 2, i)); + if (twoDigits >= 10 && twoDigits <= 26) { + dp[i] += dp[i - 2]; + } + } + return dp[s.length()]; + } +} diff --git a/maximum-product-subarray/bky373.java b/maximum-product-subarray/bky373.java new file mode 100644 index 000000000..0d325dd9e --- /dev/null +++ b/maximum-product-subarray/bky373.java @@ -0,0 +1,29 @@ +// time: O(N) +// space: O(1) +class Solution { + + public int maxProduct(int[] nums) { + double max = Integer.MIN_VALUE; + double product = 1; + + for (int num : nums) { + product *= num; + max = Math.max(product, max); + if (product == 0) { + product = 1; + } + + } + + product = 1; + for (int i = nums.length - 1; i >= 0; i--) { + product *= nums[i]; + max = Math.max(product, max); + if (product == 0) { + product = 1; + } + } + + return (int) max; + } +} diff --git a/palindromic-substrings/bky373.java b/palindromic-substrings/bky373.java new file mode 100644 index 000000000..ae8138b62 --- /dev/null +++ b/palindromic-substrings/bky373.java @@ -0,0 +1,25 @@ +// time: O(N^3) (the worst case) +// space: O(1) +class Solution { + + public int countSubstrings(String s) { + int count = 0; + for (int i = 0; i < s.length(); i++) { + for (int j = i; j < s.length(); j++) { + count += isPalindrome(s, i, j); + } + } + return count; + } + + int isPalindrome(String s, int start, int end) { + while (start < end) { + if (s.charAt(start) != s.charAt(end)) { + return 0; + } + start++; + end--; + } + return 1; + } +} diff --git a/word-break/bky373.java b/word-break/bky373.java new file mode 100644 index 000000000..fa0a20da8 --- /dev/null +++ b/word-break/bky373.java @@ -0,0 +1,31 @@ +// time: O(N^2) +// space: O(N) +class Solution { + + public boolean wordBreak(String s, List wordDict) { + Set words = new HashSet<>(wordDict); + Queue que = new LinkedList<>(); + boolean[] visited = new boolean[s.length() + 1]; + que.add(0); + + while (!que.isEmpty()) { + int start = que.remove(); + if (start == s.length()) { + return true; + } + + for (int i = start + 1; i <= s.length(); i++) { + if (visited[i]) { + continue; + } + + if (words.contains(s.substring(start, i))) { + que.add(i); + visited[i] = true; + } + } + } + + return false; + } +} From e21e2be329349f4b48ce98842855c6a04701ea96 Mon Sep 17 00:00:00 2001 From: bky373 Date: Thu, 18 Jul 2024 16:31:18 +0900 Subject: [PATCH 2/2] Add time and space complexity --- coin-change/bky373.java | 2 ++ 1 file changed, 2 insertions(+) diff --git a/coin-change/bky373.java b/coin-change/bky373.java index c09984afb..dd2bd04c2 100644 --- a/coin-change/bky373.java +++ b/coin-change/bky373.java @@ -1,3 +1,5 @@ +// time: O(n * m), where n is the amount and m is the number of coins +// space: O(n) class Solution { public int coinChange(int[] coins, int amount) {