Skip to content

Commit

Permalink
update 53 java, progress
Browse files Browse the repository at this point in the history
  • Loading branch information
yennanliu committed May 24, 2024
1 parent 2382e37 commit 0922d83
Show file tree
Hide file tree
Showing 4 changed files with 110 additions and 10 deletions.
2 changes: 1 addition & 1 deletion data/progress.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
20240524: 55
20240524: 55,54(todo),53
20240523: 56
20240522: 62,572,57
20240521: 322,191,190
Expand Down
18 changes: 9 additions & 9 deletions data/to_review.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
2024-07-18 -> ['55']
2024-07-18 -> ['55,54(todo),53']
2024-07-17 -> ['56']
2024-07-16 -> ['62,572,57']
2024-07-15 -> ['322,191,190']
Expand All @@ -13,7 +13,7 @@
2024-07-06 -> ['371']
2024-07-05 -> ['121,252']
2024-07-04 -> ['125']
2024-06-27 -> ['55']
2024-06-27 -> ['55,54(todo),53']
2024-06-26 -> ['56']
2024-06-25 -> ['62,572,57']
2024-06-24 -> ['322,191,190']
Expand All @@ -26,27 +26,27 @@
2024-06-17 -> ['105,106']
2024-06-16 -> ['242,235']
2024-06-15 -> ['371']
2024-06-14 -> ['55', '121,252']
2024-06-14 -> ['55,54(todo),53', '121,252']
2024-06-13 -> ['56', '125']
2024-06-12 -> ['62,572,57']
2024-06-11 -> ['322,191,190']
2024-06-10 -> ['73,200,70,323(again)']
2024-06-09 -> ['207,79,206,213,198']
2024-06-08 -> ['212(todo),211,338,208(again)']
2024-06-07 -> ['347,253(todo),91(todo),217']
2024-06-06 -> ['55', '226,98,253(todo)']
2024-06-06 -> ['55,54(todo),53', '226,98,253(todo)']
2024-06-05 -> ['56', '104,230,102,100']
2024-06-04 -> ['62,572,57', '105,106']
2024-06-03 -> ['322,191,190', '242,235']
2024-06-02 -> ['73,200,70,323(again)', '371']
2024-06-01 -> ['55', '207,79,206,213,198', '121,252']
2024-06-01 -> ['55,54(todo),53', '207,79,206,213,198', '121,252']
2024-05-31 -> ['56', '212(todo),211,338,208(again)', '125']
2024-05-30 -> ['62,572,57', '347,253(todo),91(todo),217']
2024-05-29 -> ['55', '322,191,190', '226,98,253(todo)']
2024-05-29 -> ['55,54(todo),53', '322,191,190', '226,98,253(todo)']
2024-05-28 -> ['56', '73,200,70,323(again)', '104,230,102,100']
2024-05-27 -> ['55', '62,572,57', '207,79,206,213,198', '105,106']
2024-05-26 -> ['55', '56', '322,191,190', '212(todo),211,338,208(again)', '242,235']
2024-05-25 -> ['55', '56', '62,572,57', '73,200,70,323(again)', '347,253(todo),91(todo),217', '371']
2024-05-27 -> ['55,54(todo),53', '62,572,57', '207,79,206,213,198', '105,106']
2024-05-26 -> ['55,54(todo),53', '56', '322,191,190', '212(todo),211,338,208(again)', '242,235']
2024-05-25 -> ['55,54(todo),53', '56', '62,572,57', '73,200,70,323(again)', '347,253(todo),91(todo),217', '371']
2024-05-24 -> ['56', '62,572,57', '322,191,190', '207,79,206,213,198', '226,98,253(todo)', '121,252']
2024-05-23 -> ['62,572,57', '322,191,190', '73,200,70,323(again)', '212(todo),211,338,208(again)', '104,230,102,100', '125']
2024-05-22 -> ['322,191,190', '73,200,70,323(again)', '207,79,206,213,198', '347,253(todo),91(todo),217', '105,106']
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,18 @@ public int maxSubArray_(int[] nums) {
int cumSum = 0;

for (int i = 0; i < nums.length; i++){
/** NOTE !!!
*
* ONLY use "cumSum + nums[i]" as condition
* to check whether give up current cumsum, and create a new one
* or keep cumsum
*/
if (cumSum + nums[i] < 0){
/**
* NOTE !!!
* if cumSum + nums[i] < 0,
* still need to get max val from (ans, cumSum + nums[i])
*/
ans = Math.max(ans, cumSum + nums[i]);
cumSum = 0;
}else{
Expand All @@ -75,6 +86,63 @@ public int maxSubArray_(int[] nums) {
return ans;
}

// V0''
// IDEA : BRUTE FORCE (by GPT)
public int maxSubArray_0(int[] nums) {
// If there is only one element, the maximum subarray is the element itself
if (nums.length == 1) {
return nums[0];
}

// Initialize variables
int res = nums[0]; // This will store the maximum subarray sum
int cur = nums[0]; // This will store the current subarray sum

// Iterate through the array starting from the second element
for (int i = 1; i < nums.length; i++) {
// Update current subarray sum
cur = Math.max(nums[i], cur + nums[i]);

// Update the result with the maximum value found so far
res = Math.max(res, cur);
}

return res;
}

// V0'''
// IDEA : GREEDY (modified by GPT)
public int maxSubArray_0_1(int[] nums) {

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

int res = nums[0];
int cur = nums[0];

// Left pointer (l) and right pointer (r) initialized to the first element
//int l = 0;
int r = 1;

while (r < nums.length) {
// If current sum is negative, reset it to the current element
if (cur < 0) {
//l = r;
cur = nums[r];
} else {
// Otherwise, add the current element to the current sum
cur += nums[r];
}
// Update the result with the maximum value found so far
res = Math.max(res, cur);
r++;
}

return res;
}


// V1
// IDEA : OPTIMIZED BRUTE FORCE (TLE)
// https://leetcode.com/problems/maximum-subarray/editorial/
Expand Down
32 changes: 32 additions & 0 deletions leetcode_java/src/main/java/dev/workspace3.java
Original file line number Diff line number Diff line change
Expand Up @@ -1522,4 +1522,36 @@ public boolean canJump(int[] nums) {
return dist >= nums.length-1; // dist < nums.length ? false : true;
}

// LC 53
// sliding window
public int maxSubArray(int[] nums) {

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

int res = -1 * Integer.MAX_VALUE;
int cur = 0;
//List<Integer> cache = new ArrayList<>();
int l = 0;
int r = 0;

while (r < nums.length){
System.out.println("l = " + l + " r = " + r + " cur = " + cur + " res = " + res);
int updated = cur + nums[r];
if (r == 0 || updated > 0) {
cur += nums[r];
r += 1;
res = Math.max(res, cur);
} else {
r += 1;
l = r;
cur = 0;
res = Math.max(res, cur);
}
}

return res;
}

}

0 comments on commit 0922d83

Please sign in to comment.