Skip to content

Commit

Permalink
add 53 java, update progress
Browse files Browse the repository at this point in the history
  • Loading branch information
yennanliu committed Nov 3, 2023
1 parent c497232 commit eb7ef40
Show file tree
Hide file tree
Showing 4 changed files with 134 additions and 4 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -974,7 +974,7 @@
## Dynamic Programming
| # | Title | Solution | Time | Space | Difficulty | Tag | Status|
|-----|---------------- | --------------- | --------------- | --------------- | ------------- |--------------|-----|
053| [Maximum Subarray](https://leetcode.com/problems/maximum-subarray/)| [Python](./leetcode_python/Dynamic_Programming/maximum-subarray.py) | _O(n)_ | _O(1)_ | easy |Curated Top 75, brute force, divied & conquer, `DP`,`dp basic`,`amazon`,`fb`| AGAIN******** (5)
053| [Maximum Subarray](https://leetcode.com/problems/maximum-subarray/)| [Python](./leetcode_python/Dynamic_Programming/maximum-subarray.py), [Java](./leetcode_java/src/main/java/LeetCodeJava/Greedy/MaximumSubarray.java) | _O(n)_ | _O(1)_ | Medium |Curated Top 75, brute force, divied & conquer, `DP`,`dp basic`,`amazon`,`fb`| AGAIN******** (5)
062| [Unique Paths](https://leetcode.com/problems/unique-paths/) | [Python](./leetcode_python/Dynamic_Programming/unique-paths.py) | _O(m * n)_| _O(m + n)_ | Medium|Curated Top 75, `dp`, `basic`, fb, google, apple, amazon, m$| AGAIN**** (2)
063| [Unique Paths II](https://leetcode.com/problems/unique-paths-ii/) | [Python](./leetcode_python/Dynamic_Programming/unique-paths-ii.py) | _O(m * n)_ | _O(m + n)_ | Medium |`trick`, check `#062 Unique Paths`, `amazon`| AGAIN*
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*
Expand Down
1 change: 1 addition & 0 deletions data/progress.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
20231103: 53
20231006: quick_sort,286
20231003: 994
20231002: 130
Expand Down
12 changes: 9 additions & 3 deletions data/to_review.txt
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
2023-12-28 -> ['53']
2023-12-07 -> ['53']
2023-11-30 -> ['quick_sort,286']
2023-11-27 -> ['994']
2023-11-25 -> ['417']
2023-11-24 -> ['133,695']
2023-11-24 -> ['53', '133,695']
2023-11-16 -> ['53']
2023-11-11 -> ['53']
2023-11-09 -> ['quick_sort,286']
2023-11-06 -> ['994']
2023-11-04 -> ['417']
2023-11-08 -> ['53']
2023-11-06 -> ['53', '994']
2023-11-05 -> ['53']
2023-11-04 -> ['53', '417']
2023-11-03 -> ['133,695']
2023-10-28 -> ['654,106,105']
2023-10-27 -> ['quick_sort,286']
Expand Down
123 changes: 123 additions & 0 deletions leetcode_java/src/main/java/LeetCodeJava/Greedy/MaximumSubarray.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
package LeetCodeJava.Greedy;

// https://leetcode.com/problems/maximum-subarray/

public class MaximumSubarray {

// V0
// IDEA : BRUTE FORCE
// https://www.bilibili.com/video/BV1aY4y1Z7ya/?share_source=copy_web&vd_source=49348a1975630e1327042905e52f488a
public int maxSubArray(int[] nums) {

if (nums.length == 0 || nums == null){
return 0;
}

if (nums.length == 1){
return nums[0];
}

// loop over nums, reset start-idx if "current sub array" < current num
// maintain the max value (ans) as well while looping

int ans = -Integer.MIN_VALUE;
int cumSum = 0;

for (int i = 0; i < nums.length; i++){
if (cumSum + nums[i] < 0){
ans = Math.max(ans, cumSum + nums[i]);
cumSum = 0;
}else{
cumSum += nums[i];
ans = Math.max(ans, cumSum);
}
}

return ans;
}

// V1
// IDEA : OPTIMIZED BRUTE FORCE (TLE)
// https://leetcode.com/problems/maximum-subarray/editorial/
public int maxSubArray_2(int[] nums) {
int maxSubarray = Integer.MIN_VALUE;
for (int i = 0; i < nums.length; i++) {
int currentSubarray = 0;
for (int j = i; j < nums.length; j++) {
currentSubarray += nums[j];
maxSubarray = Math.max(maxSubarray, currentSubarray);
}
}

return maxSubarray;
}

// V2
// IDEA : Dynamic Programming, Kadane's Algorithm
// https://leetcode.com/problems/maximum-subarray/editorial/
public int maxSubArray_3(int[] nums) {
// Initialize our variables using the first element.
int currentSubarray = nums[0];
int maxSubarray = nums[0];

// Start with the 2nd element since we already used the first one.
for (int i = 1; i < nums.length; i++) {
int num = nums[i];
// If current_subarray is negative, throw it away. Otherwise, keep adding to it.
currentSubarray = Math.max(num, currentSubarray + num);
maxSubarray = Math.max(maxSubarray, currentSubarray);
}

return maxSubarray;
}

// V3
// IDEA : Divide and Conquer
// https://leetcode.com/problems/maximum-subarray/editorial/
private int[] numsArray;

public int maxSubArray_4(int[] nums) {
numsArray = nums;

// Our helper function is designed to solve this problem for
// any array - so just call it using the entire input!
return findBestSubarray(0, numsArray.length - 1);
}

private int findBestSubarray(int left, int right) {
// Base case - empty array.
if (left > right) {
return Integer.MIN_VALUE;
}

int mid = Math.floorDiv(left + right, 2);
int curr = 0;
int bestLeftSum = 0;
int bestRightSum = 0;

// Iterate from the middle to the beginning.
for (int i = mid - 1; i >= left; i--) {
curr += numsArray[i];
bestLeftSum = Math.max(bestLeftSum, curr);
}

// Reset curr and iterate from the middle to the end.
curr = 0;
for (int i = mid + 1; i <= right; i++) {
curr += numsArray[i];
bestRightSum = Math.max(bestRightSum, curr);
}

// The bestCombinedSum uses the middle element and the best
// possible sum from each half.
int bestCombinedSum = numsArray[mid] + bestLeftSum + bestRightSum;

// Find the best subarray possible from both halves.
int leftHalf = findBestSubarray(left, mid - 1);
int rightHalf = findBestSubarray(mid + 1, right);

// The largest of the 3 is the answer for any given input array.
return Math.max(bestCombinedSum, Math.max(leftHalf, rightHalf));
}

}

0 comments on commit eb7ef40

Please sign in to comment.