Skip to content

Commit

Permalink
update 325 java
Browse files Browse the repository at this point in the history
  • Loading branch information
yennanliu committed Dec 8, 2024
1 parent 1d9efc7 commit d8af5e7
Show file tree
Hide file tree
Showing 4 changed files with 121 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 @@
20241208: 304,853
20241208: 304,853,325
20241202: 370(todo),1109(todo)
20241130: 34,767
20241126: 722,380
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 @@
2025-02-01 -> ['304,853']
2025-02-01 -> ['304,853,325']
2025-01-26 -> ['370(todo),1109(todo)']
2025-01-24 -> ['34,767']
2025-01-20 -> ['722,380']
Expand All @@ -7,32 +7,32 @@
2025-01-16 -> ['776,31']
2025-01-15 -> ['004(todo),34(todo),162(todo),275(todo)']
2025-01-14 -> ['986(todo),1229(todo),1868(todo),80(todo),209(todo),283(todo),360(todo),713(todo),532(todo),611(todo)']
2025-01-11 -> ['304,853', '394']
2025-01-11 -> ['304,853,325', '394']
2025-01-10 -> ['833,950']
2025-01-05 -> ['370(todo),1109(todo)']
2025-01-04 -> ['53,210,207']
2025-01-03 -> ['34,767', '444']
2025-01-02 -> ['1188,130,855(again)']
2024-12-30 -> ['722,380']
2024-12-29 -> ['304,853', '33,81']
2024-12-29 -> ['304,853,325', '33,81']
2024-12-28 -> ['900']
2024-12-27 -> ['253', '26,27', '802,1197,26']
2024-12-26 -> ['776,31']
2024-12-25 -> ['004(todo),34(todo),162(todo),275(todo)']
2024-12-24 -> ['986(todo),1229(todo),1868(todo),80(todo),209(todo),283(todo),360(todo),713(todo),532(todo),611(todo)']
2024-12-23 -> ['370(todo),1109(todo)']
2024-12-21 -> ['304,853', '34,767', '394', '855,846']
2024-12-21 -> ['304,853,325', '34,767', '394', '855,846']
2024-12-20 -> ['833,950', '932']
2024-12-18 -> ['951,792']
2024-12-17 -> ['722,380']
2024-12-16 -> ['304,853', '33,81']
2024-12-16 -> ['304,853,325', '33,81']
2024-12-15 -> ['370(todo),1109(todo)']
2024-12-14 -> ['253', '53,210,207', '163,1048']
2024-12-13 -> ['304,853', '34,767', '776,31', '444', '298,729']
2024-12-13 -> ['304,853,325', '34,767', '776,31', '444', '298,729']
2024-12-12 -> ['004(todo),34(todo),162(todo),275(todo)', '1188,130,855(again)', '1146']
2024-12-11 -> ['304,853', '986(todo),1229(todo),1868(todo),80(todo),209(todo),283(todo),360(todo),713(todo),532(todo),611(todo)']
2024-12-10 -> ['304,853', '370(todo),1109(todo)']
2024-12-09 -> ['304,853', '722,380']
2024-12-11 -> ['304,853,325', '986(todo),1229(todo),1868(todo),80(todo),209(todo),283(todo),360(todo),713(todo),532(todo),611(todo)']
2024-12-10 -> ['304,853,325', '370(todo),1109(todo)']
2024-12-09 -> ['304,853,325', '722,380']
2024-12-08 -> ['34,767', '33,81', '394', '737']
2024-12-07 -> ['370(todo),1109(todo)', '833,950', '900', '686,734,737']
2024-12-06 -> ['253', '26,27', '802,1197,26', '353']
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,39 @@
import java.util.HashMap;
import java.util.Map;

/**
* 325. Maximum Size Subarray Sum Equals k
* Given an array nums and a target value k, find the maximum length of a subarray that sums to k. If there isn't one, return 0 instead.
*
* Note:
* The sum of the entire nums array is guaranteed to fit within the 32-bit signed integer range.
*
* Example 1:
*
* Input: nums = [1, -1, 5, -2, 3], k = 3
* Output: 4
* Explanation: The subarray [1, -1, 5, -2] sums to 3 and is the longest.
* Example 2:
*
* Input: nums = [-2, -1, 2, 1], k = 1
* Output: 2
* Explanation: The subarray [-1, 2] sums to 1 and is the longest.
* Follow Up:
* Can you do it in O(n) time?
*
* Difficulty:
* Medium
* Lock:
* Prime
* Company:
* Amazon Facebook Google Palantir Technologies
*
*
*/
public class MaximumSizeSubarraySumEqualsK {

// V0
// TODO : implement

// V1
// https://leetcode.com/problems/maximum-size-subarray-sum-equals-k/solutions/1017059/java-prefix-sums/
Expand Down Expand Up @@ -67,4 +97,19 @@ public int maxSubArrayLen_3(int[] nums, int k) {
return max;
}

// V4
// https://leetcode.ca/2016-10-20-325-Maximum-Size-Subarray-Sum-Equals-k/
public int maxSubArrayLen_4(int[] nums, int k) {
Map<Long, Integer> d = new HashMap<>();
d.put(0L, -1);
int ans = 0;
long s = 0;
for (int i = 0; i < nums.length; ++i) {
s += nums[i];
ans = Math.max(ans, i - d.getOrDefault(s - k, i));
d.putIfAbsent(s, i);
}
return ans;
}

}
66 changes: 66 additions & 0 deletions leetcode_java/src/main/java/dev/workspace5.java
Original file line number Diff line number Diff line change
Expand Up @@ -4338,6 +4338,72 @@ public int carFleet(int target, int[] position, int[] speed){
return stack.size();
}

// LC 325
// https://leetcode.ca/all/325.html
// https://leetcode.ca/2016-10-20-325-Maximum-Size-Subarray-Sum-Equals-k/
// 4.22 - 4.45 pm
/**
* Given an array nums and a target value k,
* find the maximum length of a subarray that sums to k.
* If there isn't one, return 0 instead.
*
*
* Idea : hashmap, presum array
*
* exp 1)
* nums = [1, -1, 5, -2, 3], k = 3
*
* -> presum = [0,1,0,5,3,6] !!!!!
* -> sum(i,j) = presum[j+1] - presum[i]
* -> sum(0,3) = presum[4] = presum[0] = 3 - 0 = 3
*/
public int maxSubArrayLen(int[] nums, int k) {

// V1: presum array
// Integer[] preSumArray = new Integer[nums.length+1];
// int curSum = 0;
// preSumArray[0] = 0;
// for(int i = 1; i < nums.length; i++){
// curSum += nums[i];
// preSumArray[i] = curSum;
// }

// V2 : hash map
// map : (preSum, idx)
Map<Integer, Integer> preSumMap = new HashMap<>();
int curSum_ = 0;
for(int i = 1; i < nums.length; i++){
curSum_ += nums[i];
//preSumMap.putIfAbsent(nums[i], curSum_);
preSumMap.putIfAbsent(curSum_, i);
}


int maxSize = 0;
// TODO : optimize double loop
// for(int i = 0; i < preSumArray.length-1; i++){
// for (int j = i+1; j < preSumArray.length; j++){
// int subArraySum = preSumArray[j+1] - preSumArray[i];
// if (subArraySum == k){
// maxSize = Math.max(maxSize, j-i+1);
// }
// }
// }

// Optimized : use hashmap, check if "k-x" is in hashmap (similar as "2 sum")
for (int i = 0; i < nums.length; i++){
int val = nums[i];
// x + nums[i] = k
// -> x = k - nums[i]
if (preSumMap.containsKey(k - val)){
maxSize = Math.max(maxSize, (preSumMap.get(k-val) - i + 1));
}
}

return maxSize;
}



}

Expand Down

0 comments on commit d8af5e7

Please sign in to comment.