You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: leetcode_java/src/main/java/LeetCodeJava/HashTable/SubarraySumEqualsK.java
+68Lines changed: 68 additions & 0 deletions
Original file line number
Diff line number
Diff line change
@@ -85,6 +85,22 @@ public int subarraySum(int[] nums, int k) {
85
85
* NOTE !!!
86
86
*
87
87
* 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.
88
104
*/
89
105
map.put(0, 1);
90
106
@@ -103,6 +119,58 @@ public int subarraySum(int[] nums, int k) {
103
119
returncount;
104
120
}
105
121
122
+
// V0-1
123
+
// IDEA : HASH MAP (fixed by gpt)
124
+
publicintsubarraySum_0_1(int[] nums, intk) {
125
+
126
+
if (nums.length == 1){
127
+
if (nums[0] == k){
128
+
return1;
129
+
}
130
+
return0;
131
+
}
132
+
133
+
// map : {presum : count}
134
+
Map<Integer, Integer> map = newHashMap<>();
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
+
intpreusm = 0;
143
+
intcnt = 0;
144
+
for (inti = 0; i < nums.length; i++){
145
+
intcur = 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.
Copy file name to clipboardExpand all lines: leetcode_java/src/main/java/dev/workspace5.java
+62-13Lines changed: 62 additions & 13 deletions
Original file line number
Diff line number
Diff line change
@@ -4516,34 +4516,83 @@ public boolean checkSubarraySum(int[] nums, int k) {
4516
4516
*/
4517
4517
publicintsubarraySum(int[] nums, intk) {
4518
4518
4519
-
if(nums.length==1){
4520
-
returnnums[0] == k ? 1 : 0;
4519
+
if (nums.length == 1){
4520
+
if (nums[0] == k){
4521
+
return1;
4522
+
}
4523
+
return0;
4521
4524
}
4522
4525
4523
-
// map : {presum : idx}
4526
+
// map : {presum : count}
4524
4527
Map<Integer, Integer> map = newHashMap<>();
4525
-
intpresum = 0;
4528
+
/** NOTE !!!
4529
+
*
4530
+
* init map as below
4531
+
*/
4532
+
map.put(0,1); // TODO : check
4533
+
intpreusm = 0;
4526
4534
intcnt = 0;
4527
-
map.put(0, -1); // TODO : check if necessary
4528
4535
for (inti = 0; i < nums.length; i++){
4529
4536
intcur = nums[i];
4530
-
presum += cur;
4531
-
map.putIfAbsent(presum, i); // ???
4537
+
preusm += cur;
4532
4538
/**
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.
4534
4552
*
4535
-
* k = presum(j+1) - presum(i)
4536
-
* -> presum(i) = presum(j+1) - k
4537
4553
*/
4538
-
if (map.containsKey(presum - k) && map.get(presum - k) == i+1){
0 commit comments