Skip to content

Commit d8af5e7

Browse files
committed
update 325 java
1 parent 1d9efc7 commit d8af5e7

File tree

4 files changed

+121
-10
lines changed

4 files changed

+121
-10
lines changed

data/progress.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
20241208: 304,853
1+
20241208: 304,853,325
22
20241202: 370(todo),1109(todo)
33
20241130: 34,767
44
20241126: 722,380

data/to_review.txt

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
2025-02-01 -> ['304,853']
1+
2025-02-01 -> ['304,853,325']
22
2025-01-26 -> ['370(todo),1109(todo)']
33
2025-01-24 -> ['34,767']
44
2025-01-20 -> ['722,380']
@@ -7,32 +7,32 @@
77
2025-01-16 -> ['776,31']
88
2025-01-15 -> ['004(todo),34(todo),162(todo),275(todo)']
99
2025-01-14 -> ['986(todo),1229(todo),1868(todo),80(todo),209(todo),283(todo),360(todo),713(todo),532(todo),611(todo)']
10-
2025-01-11 -> ['304,853', '394']
10+
2025-01-11 -> ['304,853,325', '394']
1111
2025-01-10 -> ['833,950']
1212
2025-01-05 -> ['370(todo),1109(todo)']
1313
2025-01-04 -> ['53,210,207']
1414
2025-01-03 -> ['34,767', '444']
1515
2025-01-02 -> ['1188,130,855(again)']
1616
2024-12-30 -> ['722,380']
17-
2024-12-29 -> ['304,853', '33,81']
17+
2024-12-29 -> ['304,853,325', '33,81']
1818
2024-12-28 -> ['900']
1919
2024-12-27 -> ['253', '26,27', '802,1197,26']
2020
2024-12-26 -> ['776,31']
2121
2024-12-25 -> ['004(todo),34(todo),162(todo),275(todo)']
2222
2024-12-24 -> ['986(todo),1229(todo),1868(todo),80(todo),209(todo),283(todo),360(todo),713(todo),532(todo),611(todo)']
2323
2024-12-23 -> ['370(todo),1109(todo)']
24-
2024-12-21 -> ['304,853', '34,767', '394', '855,846']
24+
2024-12-21 -> ['304,853,325', '34,767', '394', '855,846']
2525
2024-12-20 -> ['833,950', '932']
2626
2024-12-18 -> ['951,792']
2727
2024-12-17 -> ['722,380']
28-
2024-12-16 -> ['304,853', '33,81']
28+
2024-12-16 -> ['304,853,325', '33,81']
2929
2024-12-15 -> ['370(todo),1109(todo)']
3030
2024-12-14 -> ['253', '53,210,207', '163,1048']
31-
2024-12-13 -> ['304,853', '34,767', '776,31', '444', '298,729']
31+
2024-12-13 -> ['304,853,325', '34,767', '776,31', '444', '298,729']
3232
2024-12-12 -> ['004(todo),34(todo),162(todo),275(todo)', '1188,130,855(again)', '1146']
33-
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)']
34-
2024-12-10 -> ['304,853', '370(todo),1109(todo)']
35-
2024-12-09 -> ['304,853', '722,380']
33+
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)']
34+
2024-12-10 -> ['304,853,325', '370(todo),1109(todo)']
35+
2024-12-09 -> ['304,853,325', '722,380']
3636
2024-12-08 -> ['34,767', '33,81', '394', '737']
3737
2024-12-07 -> ['370(todo),1109(todo)', '833,950', '900', '686,734,737']
3838
2024-12-06 -> ['253', '26,27', '802,1197,26', '353']

leetcode_java/src/main/java/LeetCodeJava/HashTable/MaximumSizeSubarraySumEqualsK.java

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,39 @@
77
import java.util.HashMap;
88
import java.util.Map;
99

10+
/**
11+
* 325. Maximum Size Subarray Sum Equals k
12+
* 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.
13+
*
14+
* Note:
15+
* The sum of the entire nums array is guaranteed to fit within the 32-bit signed integer range.
16+
*
17+
* Example 1:
18+
*
19+
* Input: nums = [1, -1, 5, -2, 3], k = 3
20+
* Output: 4
21+
* Explanation: The subarray [1, -1, 5, -2] sums to 3 and is the longest.
22+
* Example 2:
23+
*
24+
* Input: nums = [-2, -1, 2, 1], k = 1
25+
* Output: 2
26+
* Explanation: The subarray [-1, 2] sums to 1 and is the longest.
27+
* Follow Up:
28+
* Can you do it in O(n) time?
29+
*
30+
* Difficulty:
31+
* Medium
32+
* Lock:
33+
* Prime
34+
* Company:
35+
* Amazon Facebook Google Palantir Technologies
36+
*
37+
*
38+
*/
1039
public class MaximumSizeSubarraySumEqualsK {
1140

1241
// V0
42+
// TODO : implement
1343

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

100+
// V4
101+
// https://leetcode.ca/2016-10-20-325-Maximum-Size-Subarray-Sum-Equals-k/
102+
public int maxSubArrayLen_4(int[] nums, int k) {
103+
Map<Long, Integer> d = new HashMap<>();
104+
d.put(0L, -1);
105+
int ans = 0;
106+
long s = 0;
107+
for (int i = 0; i < nums.length; ++i) {
108+
s += nums[i];
109+
ans = Math.max(ans, i - d.getOrDefault(s - k, i));
110+
d.putIfAbsent(s, i);
111+
}
112+
return ans;
113+
}
114+
70115
}

leetcode_java/src/main/java/dev/workspace5.java

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4338,6 +4338,72 @@ public int carFleet(int target, int[] position, int[] speed){
43384338
return stack.size();
43394339
}
43404340

4341+
// LC 325
4342+
// https://leetcode.ca/all/325.html
4343+
// https://leetcode.ca/2016-10-20-325-Maximum-Size-Subarray-Sum-Equals-k/
4344+
// 4.22 - 4.45 pm
4345+
/**
4346+
* Given an array nums and a target value k,
4347+
* find the maximum length of a subarray that sums to k.
4348+
* If there isn't one, return 0 instead.
4349+
*
4350+
*
4351+
* Idea : hashmap, presum array
4352+
*
4353+
* exp 1)
4354+
* nums = [1, -1, 5, -2, 3], k = 3
4355+
*
4356+
* -> presum = [0,1,0,5,3,6] !!!!!
4357+
* -> sum(i,j) = presum[j+1] - presum[i]
4358+
* -> sum(0,3) = presum[4] = presum[0] = 3 - 0 = 3
4359+
*/
4360+
public int maxSubArrayLen(int[] nums, int k) {
4361+
4362+
// V1: presum array
4363+
// Integer[] preSumArray = new Integer[nums.length+1];
4364+
// int curSum = 0;
4365+
// preSumArray[0] = 0;
4366+
// for(int i = 1; i < nums.length; i++){
4367+
// curSum += nums[i];
4368+
// preSumArray[i] = curSum;
4369+
// }
4370+
4371+
// V2 : hash map
4372+
// map : (preSum, idx)
4373+
Map<Integer, Integer> preSumMap = new HashMap<>();
4374+
int curSum_ = 0;
4375+
for(int i = 1; i < nums.length; i++){
4376+
curSum_ += nums[i];
4377+
//preSumMap.putIfAbsent(nums[i], curSum_);
4378+
preSumMap.putIfAbsent(curSum_, i);
4379+
}
4380+
4381+
4382+
int maxSize = 0;
4383+
// TODO : optimize double loop
4384+
// for(int i = 0; i < preSumArray.length-1; i++){
4385+
// for (int j = i+1; j < preSumArray.length; j++){
4386+
// int subArraySum = preSumArray[j+1] - preSumArray[i];
4387+
// if (subArraySum == k){
4388+
// maxSize = Math.max(maxSize, j-i+1);
4389+
// }
4390+
// }
4391+
// }
4392+
4393+
// Optimized : use hashmap, check if "k-x" is in hashmap (similar as "2 sum")
4394+
for (int i = 0; i < nums.length; i++){
4395+
int val = nums[i];
4396+
// x + nums[i] = k
4397+
// -> x = k - nums[i]
4398+
if (preSumMap.containsKey(k - val)){
4399+
maxSize = Math.max(maxSize, (preSumMap.get(k-val) - i + 1));
4400+
}
4401+
}
4402+
4403+
return maxSize;
4404+
}
4405+
4406+
43414407

43424408
}
43434409

0 commit comments

Comments
 (0)