Skip to content

[bky373] Add Week 12 Solutions #195

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 4 commits into from
Jul 23, 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()];
}
}
15 changes: 15 additions & 0 deletions jump-game/bky373.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// time: O(N)
// space: O(1)
class Solution {

public boolean canJump(int[] nums) {
int lastPosition = nums.length - 1;

for (int i = nums.length - 1; i >= 0; i--) {
if (i + nums[i] >= lastPosition) {
lastPosition = i;
}
}
return lastPosition == 0;
}
}
20 changes: 20 additions & 0 deletions longest-common-subsequence/bky373.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// time: O(m*n)
// space: O(m*n)
class Solution {

public int longestCommonSubsequence(String text1, String text2) {
int[][] dp = new int[text1.length() + 1][text2.length() + 1];

for (int col = text2.length() - 1; col >= 0; col--) {
for (int row = text1.length() - 1; row >= 0; row--) {
if (text1.charAt(row) == text2.charAt(col)) {
dp[row][col] = 1 + dp[row + 1][col + 1];
} else {
dp[row][col] = Math.max(dp[row + 1][col], dp[row][col + 1]);
}
}
}

return dp[0][0];
}
}
24 changes: 24 additions & 0 deletions longest-increasing-subsequence/bky373.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// time: O(N^2)
// space: O(N)
class Solution {

public int lengthOfLIS(int[] nums) {
int[] dp = new int[nums.length];
Arrays.fill(dp, 1);

for (int i = 1; i < nums.length; i++) {
for (int j = 0; j < i; j++) {
if (nums[i] > nums[j]) {
dp[i] = Math.max(dp[i], dp[j] + 1);
}
}
}

int longest = 0;
for (int count : dp) {
longest = Math.max(longest, count);
}

return longest;
}
}
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;
}
}
17 changes: 17 additions & 0 deletions maximum-subarray/bky373.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// time: O(N)
// space: O(1)
class Solution {

public int maxSubArray(int[] nums) {
int currentSum = nums[0];
int maxSum = nums[0];

for (int i = 1; i < nums.length; i++) {
int k = nums[i];
currentSum = Math.max(k, currentSum + k);
maxSum = Math.max(maxSum, currentSum);
}

return maxSum;
}
}
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;
}
}
47 changes: 47 additions & 0 deletions unique-paths/bky373.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
[Approch 1]
- time: O(m*n)
- space: O(m*n)
*/
class Solution {

public int uniquePaths(int m, int n) {
int[][] dp = new int[m][n];

for (int[] row : dp) {
Arrays.fill(row, 1);
}

for (int row = 1; row < m; row++) {
for (int col = 1; col < n; col++) {
dp[row][col] = dp[row - 1][col] + dp[row][col - 1];
}
}

return dp[m - 1][n - 1];
}
}

/*
[Approch 2]
- time: O(m*n)
- space: O(n)
*/
class Solution {

public int uniquePaths(int m, int n) {
int[] upper = new int[n];

Arrays.fill(upper, 1);

for (int i = 1; i < m; i++) {
int left = 1;
for (int j = 1; j < n; j++) {
left += upper[j];
upper[j] = left;
}
}

return upper[n - 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;
}
}