Skip to content

[bky373] Add Week 11 Solutions #191

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jul 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions coin-change/bky373.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// 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) {
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];
}
}
22 changes: 22 additions & 0 deletions decode-ways/bky373.java
Original file line number Diff line number Diff line change
@@ -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()];
}
}
29 changes: 29 additions & 0 deletions maximum-product-subarray/bky373.java
Original file line number Diff line number Diff line change
@@ -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;
}
}
25 changes: 25 additions & 0 deletions palindromic-substrings/bky373.java
Original file line number Diff line number Diff line change
@@ -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;
}
}
31 changes: 31 additions & 0 deletions word-break/bky373.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// time: O(N^2)
// space: O(N)
class Solution {

public boolean wordBreak(String s, List<String> wordDict) {
Set<String> words = new HashSet<>(wordDict);
Queue<Integer> 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;
}
}