diff --git a/doc/cheatsheet/prefix_sum.md b/doc/cheatsheet/prefix_sum.md index 0d81a847..53db6b10 100644 --- a/doc/cheatsheet/prefix_sum.md +++ b/doc/cheatsheet/prefix_sum.md @@ -28,6 +28,7 @@ We can use prefix sums. Say P[i+1] = A[0] + A[1] + ... + A[i], where A[i] = 1 if - LC 370 - sub array - sum array sum == k + - pre sum count - LC 560 - Maximum Size Subarray Sum Equals k - LC 325 @@ -225,7 +226,51 @@ class Solution(object): return result ``` -### 2-5) Continuous Subarray Sum +### 2-5) Subarray Sum Equals K + +```java +// java +// LC 560 +public int subarraySum(int[] nums, int k) { + /** + * NOTE !!! + * + * use Map to store prefix sum and its count + * + * map : {prefixSum: count} + * + * + * -> since "same preSum may have multiple combination" within hashMap, + * so it's needed to track preSum COUNT, instead of its index + */ + Map map = new HashMap<>(); + int presum = 0; + int count = 0; + + /** + * NOTE !!! + * + * Initialize the map with prefix sum 0 (to handle subarrays starting at index 0) + */ + map.put(0, 1); + + for (int num : nums) { + presum += num; + + // Check if there's a prefix sum such that presum - k exists + if (map.containsKey(presum - k)) { + count += map.get(presum - k); + } + + // Update the map with the current prefix sum + map.put(presum, map.getOrDefault(presum, 0) + 1); + } + + return count; +} +``` + +### 2-6) Continuous Subarray Sum ```java // java diff --git a/leetcode_java/src/main/java/LeetCodeJava/HashTable/SubarraySumEqualsK.java b/leetcode_java/src/main/java/LeetCodeJava/HashTable/SubarraySumEqualsK.java index 0ea5cb9c..0ea9fbc8 100644 --- a/leetcode_java/src/main/java/LeetCodeJava/HashTable/SubarraySumEqualsK.java +++ b/leetcode_java/src/main/java/LeetCodeJava/HashTable/SubarraySumEqualsK.java @@ -66,12 +66,26 @@ public class SubarraySumEqualsK { * • Removed unnecessary index-based conditions (map.get(presum - k) == i + 1) which were incorrect and not required for this problem. */ public int subarraySum(int[] nums, int k) { - // Map to store prefix sum and its count + /** + * NOTE !!! + * + * use Map to store prefix sum and its count + * + * map : {prefixSum: count} + * + * + * -> since "same preSum may have multiple combination" within hashMap, + * so it's needed to track preSum COUNT, instead of its index + */ Map map = new HashMap<>(); int presum = 0; int count = 0; - // Initialize the map with prefix sum 0 (to handle subarrays starting at index 0) + /** + * NOTE !!! + * + * Initialize the map with prefix sum 0 (to handle subarrays starting at index 0) + */ map.put(0, 1); for (int num : nums) {