Skip to content

Commit

Permalink
update 053 java
Browse files Browse the repository at this point in the history
  • Loading branch information
yennanliu committed Dec 11, 2023
1 parent b0d99a5 commit eeb5747
Show file tree
Hide file tree
Showing 4 changed files with 105 additions and 11 deletions.
2 changes: 1 addition & 1 deletion data/progress.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
20231211: 39
20231211: 39,53
20231208: 33
20231206: 19,003
20231205: 23
Expand Down
18 changes: 9 additions & 9 deletions data/to_review.txt
Original file line number Diff line number Diff line change
@@ -1,26 +1,26 @@
2024-02-04 -> ['39']
2024-02-04 -> ['39,53']
2024-01-30 -> ['19,003']
2024-01-29 -> ['23']
2024-01-28 -> ['55,45']
2024-01-14 -> ['39']
2024-01-14 -> ['39,53']
2024-01-09 -> ['19,003']
2024-01-08 -> ['23']
2024-01-07 -> ['55,45']
2024-01-02 -> ['452,406,135']
2024-01-01 -> ['39']
2024-01-01 -> ['39,53']
2023-12-31 -> ['134']
2023-12-28 -> ['53']
2023-12-27 -> ['19,003']
2023-12-26 -> ['23']
2023-12-25 -> ['55,45']
2023-12-24 -> ['39']
2023-12-19 -> ['39', '19,003']
2023-12-24 -> ['39,53']
2023-12-19 -> ['39,53', '19,003']
2023-12-18 -> ['23']
2023-12-17 -> ['55,45']
2023-12-16 -> ['39']
2023-12-14 -> ['39', '19,003']
2023-12-13 -> ['39', '23']
2023-12-12 -> ['39', '55,45', '452,406,135']
2023-12-16 -> ['39,53']
2023-12-14 -> ['39,53', '19,003']
2023-12-13 -> ['39,53', '23']
2023-12-12 -> ['39,53', '55,45', '452,406,135']
2023-12-11 -> ['19,003']
2023-12-10 -> ['23', '134']
2023-12-09 -> ['19,003', '55,45']
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,48 @@
public class MaximumSubarray {

// V0
// IDEA : BRUTE FORCE + SUB ARRAY
public int maxSubArray(int[] nums) {

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

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

int cumsum = 0;
int maxSum = -1 * Integer.MAX_VALUE;

for (int i = 0; i < nums.length; i++){
int cur = nums[i];
// NOTE !!! only add cur if cumsum + cur >= 0
// -> e.g. add cur element to cur sub array
if (cumsum + cur >= 0){
cumsum = cumsum + cur;
maxSum = Math.max(maxSum, cumsum);
/**
* NOTE !!!
* need to consider "negative all input cases" (Math.max(maxSum, cur);)
* e.g. nums = [-1, -2]
* -> so we get max between maxSum and cur
* -> since whatever sub array sum in this case is < 0
* -> so we choose the "biggest" negative element in nums
*/
}else{
maxSum = Math.max(maxSum, cur);
cumsum = 0;
}
}

return maxSum;
}

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

if (nums.length == 0 || nums == null){
return 0;
Expand Down
55 changes: 55 additions & 0 deletions leetcode_java/src/main/java/dev/Test1.java
Original file line number Diff line number Diff line change
Expand Up @@ -383,5 +383,60 @@ private int getSum(List<Integer> input){
return res;
}

/**
* exp 1
* nums = [-2,1,-3,4,-1,2,1,-5,4]
*
* sub :
* [-2]
* [1], cumsum = -2+1 < 0, remove -2, max = 1
* [] cumsum = 1 + (-3) < 0, remove 1, -3, max = 1
* [4], max = 4
* [ 4, -1], max = 4
* [4, -1, 2], max = 5
* [4, -1, 2, 1], max = 6
* [4, -1, 2, 1, -5], max =6
* [4, -1, 2, 1, -5, -4], max = 6
*
*
* exp 2
*
* nums = [5,4,-1,7,8]
*
* [5], max = 5
* [5, 4], max = 9
* [5,4,-1], max = 9
* [5,4,-1, 7], max = 15
* [5,4,-1, 7, 8], max = 23
*
*/

// 2 pointers
// https://leetcode.com/problems/maximum-subarray/
public int maxSubArray(int[] nums) {

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

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

int cumsum = 0;
int maxSum = -1 * Integer.MAX_VALUE;

for (int i = 0; i < nums.length; i++){
int cur = nums[i];
if (cumsum + cur >= 0){
cumsum = cumsum + cur;
maxSum = Math.max(maxSum, cumsum);
}else{
maxSum = Math.max(maxSum, cur);
cumsum = 0;
}
}

return maxSum;
}
}

0 comments on commit eeb5747

Please sign in to comment.