Skip to content

Commit abf7df1

Browse files
committed
update 560 java
1 parent 6897c23 commit abf7df1

File tree

3 files changed

+131
-14
lines changed

3 files changed

+131
-14
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ public boolean checkSubarraySum_0_1(int[] nums, int k) {
128128
*
129129
* -> idea 1) (presum + cur) % k - (presum) = 0, so it's k multiple
130130
*
131-
*
131+
*
132132
*/
133133
presum = (presum + cur) % k;
134134
// NOTE !!! below

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

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,22 @@ public int subarraySum(int[] nums, int k) {
8585
* NOTE !!!
8686
*
8787
* Initialize the map with prefix sum 0 (to handle subarrays starting at index 0)
88+
*
89+
*
90+
*
91+
* Purpose of map.put(0, 1);
92+
*
93+
* 1. Handle the Initial Case:
94+
* • The prefix sum presum starts at 0 before any elements of the array are processed.
95+
* • Adding map.put(0, 1) ensures that if a subarray’s prefix sum equals k (e.g., the subarray itself equals k ), it is counted correctly.
96+
*
97+
* 2. Account for Subarrays Starting at Index 0:
98+
* • Consider the case where the cumulative sum of elements up to a certain index j equals k : presum[j] = k
99+
* • The subarray from index 0 to j should count as a valid subarray.
100+
* • To check this condition, the code calculates presum - k and looks for it in the map. For subarrays starting at index 0, presum - k equals 0. Adding map.put(0, 1) ensures this case is handled properly.
101+
*
102+
* 3. Count Prefix Sums:
103+
* • The value 1 in map.put(0, 1) represents the fact that there is one prefix sum of 0 initially (before processing any elements). This allows the algorithm to correctly count subarrays that sum to k as the prefix sum progresses.
88104
*/
89105
map.put(0, 1);
90106

@@ -103,6 +119,58 @@ public int subarraySum(int[] nums, int k) {
103119
return count;
104120
}
105121

122+
// V0-1
123+
// IDEA : HASH MAP (fixed by gpt)
124+
public int subarraySum_0_1(int[] nums, int k) {
125+
126+
if (nums.length == 1){
127+
if (nums[0] == k){
128+
return 1;
129+
}
130+
return 0;
131+
}
132+
133+
// map : {presum : count}
134+
Map<Integer, Integer> map = new HashMap<>();
135+
/** NOTE !!!
136+
*
137+
* init map as below
138+
*
139+
* Initialize the map with prefix sum 0 (to handle subarrays starting at index 0)
140+
*/
141+
map.put(0,1);
142+
int preusm = 0;
143+
int cnt = 0;
144+
for (int i = 0; i < nums.length; i++){
145+
int cur = nums[i];
146+
preusm += cur;
147+
/**
148+
* Reason why update map after `count` update (e.g. map.put(preusm, map.getOrDefault(preusm, 0) + 1) after if condition)
149+
*
150+
* 1. Avoid Overcounting:
151+
* • When checking if (map.containsKey(preusm - k)), you are looking for how many previous subarrays have a prefix sum of preusm - k.
152+
* • If you update the map before this check (i.e., increment the count for the current preusm), you might mistakenly count the current subarray itself in this operation, leading to incorrect results.
153+
*
154+
* 2. Logical Order of Operations:
155+
* • The purpose of the map is to store the counts of previous prefix sums seen so far.
156+
* • When you calculate cnt += map.get(preusm - k), you are determining how many times the subarray sum k has been encountered up to this point.
157+
* • Only after this check should you update the map to include the current preusm for subsequent iterations.
158+
*
159+
* 3. Current Subarray Shouldn’t Influence Itself:
160+
* • In the current iteration, the subarray being evaluated shouldn’t count itself as contributing to the result. By updating the map after the check, you ensure the current prefix sum becomes available only for future iterations.
161+
*
162+
*/
163+
//map.put(preusm, map.getOrDefault(preusm, 0) + 1);
164+
if (map.containsKey(preusm - k)){
165+
cnt += map.get(preusm - k);
166+
}
167+
// NOTE !! update map after `if condition`
168+
map.put(preusm, map.getOrDefault(preusm, 0) + 1);
169+
}
170+
171+
return cnt;
172+
}
173+
106174
// V1
107175
// IDEA : HASH MAP
108176
// https://leetcode.com/problems/subarray-sum-equals-k/solutions/6143642/java-beats-9983-by-mohamedhazem3-9yj6/

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

Lines changed: 62 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4516,34 +4516,83 @@ public boolean checkSubarraySum(int[] nums, int k) {
45164516
*/
45174517
public int subarraySum(int[] nums, int k) {
45184518

4519-
if(nums.length==1){
4520-
return nums[0] == k ? 1 : 0;
4519+
if (nums.length == 1){
4520+
if (nums[0] == k){
4521+
return 1;
4522+
}
4523+
return 0;
45214524
}
45224525

4523-
// map : {presum : idx}
4526+
// map : {presum : count}
45244527
Map<Integer, Integer> map = new HashMap<>();
4525-
int presum = 0;
4528+
/** NOTE !!!
4529+
*
4530+
* init map as below
4531+
*/
4532+
map.put(0,1); // TODO : check
4533+
int preusm = 0;
45264534
int cnt = 0;
4527-
map.put(0, -1); // TODO : check if necessary
45284535
for (int i = 0; i < nums.length; i++){
45294536
int cur = nums[i];
4530-
presum += cur;
4531-
map.putIfAbsent(presum, i); // ???
4537+
preusm += cur;
45324538
/**
4533-
* sum(i,j) = presum(j+1) - presum(i)
4539+
* Reason why update map after `count` update (e.g. map.put(preusm, map.getOrDefault(preusm, 0) + 1) after if condition)
4540+
*
4541+
* 1. Avoid Overcounting:
4542+
* • When checking if (map.containsKey(preusm - k)), you are looking for how many previous subarrays have a prefix sum of preusm - k.
4543+
* • If you update the map before this check (i.e., increment the count for the current preusm), you might mistakenly count the current subarray itself in this operation, leading to incorrect results.
4544+
*
4545+
* 2. Logical Order of Operations:
4546+
* • The purpose of the map is to store the counts of previous prefix sums seen so far.
4547+
* • When you calculate cnt += map.get(preusm - k), you are determining how many times the subarray sum k has been encountered up to this point.
4548+
* • Only after this check should you update the map to include the current preusm for subsequent iterations.
4549+
*
4550+
* 3. Current Subarray Shouldn’t Influence Itself:
4551+
* • In the current iteration, the subarray being evaluated shouldn’t count itself as contributing to the result. By updating the map after the check, you ensure the current prefix sum becomes available only for future iterations.
45344552
*
4535-
* k = presum(j+1) - presum(i)
4536-
* -> presum(i) = presum(j+1) - k
45374553
*/
4538-
if (map.containsKey(presum - k) && map.get(presum - k) == i+1){
4539-
cnt += 1;
4554+
//map.put(preusm, map.getOrDefault(preusm, 0) + 1);
4555+
if (map.containsKey(preusm - k)){
4556+
cnt += map.get(preusm - k);
45404557
}
4541-
//map.putIfAbsent(presum, i);
4558+
// NOTE !! update map after `if condition`
4559+
map.put(preusm, map.getOrDefault(preusm, 0) + 1);
45424560
}
45434561

45444562
return cnt;
45454563
}
45464564

4565+
4566+
// public int subarraySum(int[] nums, int k) {
4567+
//
4568+
// if(nums.length==1){
4569+
// return nums[0] == k ? 1 : 0;
4570+
// }
4571+
//
4572+
// // map : {presum : idx}
4573+
// Map<Integer, Integer> map = new HashMap<>();
4574+
// int presum = 0;
4575+
// int cnt = 0;
4576+
// map.put(0, -1); // TODO : check if necessary
4577+
// for (int i = 0; i < nums.length; i++){
4578+
// int cur = nums[i];
4579+
// presum += cur;
4580+
// map.putIfAbsent(presum, i); // ???
4581+
// /**
4582+
// * sum(i,j) = presum(j+1) - presum(i)
4583+
// *
4584+
// * k = presum(j+1) - presum(i)
4585+
// * -> presum(i) = presum(j+1) - k
4586+
// */
4587+
// if (map.containsKey(presum - k) && map.get(presum - k) == i+1){
4588+
// cnt += 1;
4589+
// }
4590+
// //map.putIfAbsent(presum, i);
4591+
// }
4592+
//
4593+
// return cnt;
4594+
// }
4595+
45474596
// LC 1109
45484597
// https://leetcode.com/problems/corporate-flight-bookings/
45494598
public int[] corpFlightBookings(int[][] bookings, int n) {

0 commit comments

Comments
 (0)