Skip to content

Commit 0069e19

Browse files
committed
Add Week 12 Solutions
1 parent e21e2be commit 0069e19

File tree

5 files changed

+96
-0
lines changed

5 files changed

+96
-0
lines changed

jump-game/bky373.java

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// time: O(N)
2+
// space: O(1)
3+
class Solution {
4+
5+
public boolean canJump(int[] nums) {
6+
int lastPosition = nums.length - 1;
7+
8+
for (int i = nums.length - 1; i >= 0; i--) {
9+
if (i + nums[i] >= lastPosition) {
10+
lastPosition = i;
11+
}
12+
}
13+
return lastPosition == 0;
14+
}
15+
}
+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// time: O(m*n)
2+
// space: O(m*n)
3+
class Solution {
4+
5+
public int longestCommonSubsequence(String text1, String text2) {
6+
int[][] dp = new int[text1.length() + 1][text2.length() + 1];
7+
8+
for (int col = text2.length() - 1; col >= 0; col--) {
9+
for (int row = text1.length() - 1; row >= 0; row--) {
10+
if (text1.charAt(row) == text2.charAt(col)) {
11+
dp[row][col] = 1 + dp[row + 1][col + 1];
12+
} else {
13+
dp[row][col] = Math.max(dp[row + 1][col], dp[row][col + 1]);
14+
}
15+
}
16+
}
17+
18+
return dp[0][0];
19+
}
20+
}
+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// time: O(N^2)
2+
// space: O(N)
3+
class Solution {
4+
5+
public int lengthOfLIS(int[] nums) {
6+
int[] dp = new int[nums.length];
7+
Arrays.fill(dp, 1);
8+
9+
for (int i = 1; i < nums.length; i++) {
10+
for (int j = 0; j < i; j++) {
11+
if (nums[i] > nums[j]) {
12+
dp[i] = Math.max(dp[i], dp[j] + 1);
13+
}
14+
}
15+
}
16+
17+
int longest = 0;
18+
for (int count : dp) {
19+
longest = Math.max(longest, count);
20+
}
21+
22+
return longest;
23+
}
24+
}

maximum-subarray/bky373.java

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// time: O(N)
2+
// space: O(1)
3+
class Solution {
4+
5+
public int maxSubArray(int[] nums) {
6+
int currentSum = nums[0];
7+
int maxSum = nums[0];
8+
9+
for (int i = 1; i < nums.length; i++) {
10+
int k = nums[i];
11+
currentSum = Math.max(k, currentSum + k);
12+
maxSum = Math.max(maxSum, currentSum);
13+
}
14+
15+
return maxSum;
16+
}
17+
}

unique-paths/bky373.java

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// time: O(m*n)
2+
// space: O(m*n)
3+
class Solution {
4+
5+
public int uniquePaths(int m, int n) {
6+
int[][] dp = new int[m][n];
7+
8+
for (int[] row : dp) {
9+
Arrays.fill(row, 1);
10+
}
11+
12+
for (int row = 1; row < m; row++) {
13+
for (int col = 1; col < n; col++) {
14+
dp[row][col] = dp[row - 1][col] + dp[row][col - 1];
15+
}
16+
}
17+
18+
return dp[m - 1][n - 1];
19+
}
20+
}

0 commit comments

Comments
 (0)