From b26215cb4eb477fa98845fa5297db4cb634df48f Mon Sep 17 00:00:00 2001 From: yennanliu Date: Sat, 14 Dec 2024 11:46:31 +0800 Subject: [PATCH] add 560 java --- README.md | 2 +- data/progress.txt | 2 +- data/to_review.txt | 18 +-- .../HashTable/SubarraySumEqualsK.java | 151 ++++++++++++++++++ .../src/main/java/dev/workspace5.java | 40 +++++ 5 files changed, 202 insertions(+), 11 deletions(-) create mode 100644 leetcode_java/src/main/java/LeetCodeJava/HashTable/SubarraySumEqualsK.java diff --git a/README.md b/README.md index 0f16d650..b63c5f5e 100644 --- a/README.md +++ b/README.md @@ -371,7 +371,7 @@ 525| [Contiguous Array](https://leetcode.com/problems/contiguous-array/) | [Python](./leetcode_python/Hash_table/contiguous-array.py) | _O(n)_ | _O(n)_ | Medium | sub-array sum, `good basic`, `array`, `hashmap`, `cache`, `AGAIN`, `fb`, amazon| AGAIN*************** (11) (MUST) 532| [K-diff Pairs in an Array](https://leetcode.com/problems/k-diff-pairs-in-an-array/) | [Python ](./leetcode_python/Hash_table/k-diff-pairs-in-an-array.py) | _O(n)_ | _O(n)_ | Medium |hash table, `basic`, `collections.Counter()`,`a-b =k -> a = k + b `, `amazon`| AGAIN********** (6) 554| [Brick Wall](https://leetcode.com/problems/brick-wall/) | [Python ](./leetcode_python/Hash_table/brick-wall.py) | _O(n)_ | _O(m)_ | Medium |`trick`,`hash map`, `bloomberg`, `fb`, grab | AGAIN******** (5) -560| [Subarray Sum Equals K](https://leetcode.com/problems/subarray-sum-equals-k/) | [Python ](./leetcode_python/Hash_table/subarray-sum-equals-k.py) | _O(n)_ | _O(n)_ | Medium | `must check`,check `# 523 Continuous Subarray Sum`, LC 1268,`basic`, `substring`,`good trick`,`google`,`fb`| AGAIN************** (6) (MUST) +560| [Subarray Sum Equals K](https://leetcode.com/problems/subarray-sum-equals-k/) | [Python ](./leetcode_python/Hash_table/subarray-sum-equals-k.py), [Java](./leetcode_java/src/main/java/LeetCodeJava/HashTable/SubarraySumEqualsK.java) | _O(n)_ | _O(n)_ | Medium | `must check`,check `# 523 Continuous Subarray Sum`, LC 1268,`basic`, `substring`,`good trick`,`google`,`fb`| AGAIN************** (6) (MUST) 561| [Array Partition I](https://leetcode.com/problems/array-partition-i/) | [Python ](./leetcode_python/Hash_table/array-partition-i.py) | _O(r)_ | _O(r)_ | Easy | | OK 575| [Distribute Candies](https://leetcode.com/problems/distribute-candies/) | [Python ](./leetcode_python/Hash_table/distribute-candies.py) | _O(n)_ | _O(n)_ | Easy | | OK 594| [Longest Harmonious Subsequence](https://leetcode.com/problems/longest-harmonious-subsequence/) | [Python ](./leetcode_python/Hash_table/longest-harmonious-subsequence.py) | _O(n)_ | _O(n)_ | Easy | `basic`, `good trick`| OK* (3) diff --git a/data/progress.txt b/data/progress.txt index 228d7ca5..824857b2 100644 --- a/data/progress.txt +++ b/data/progress.txt @@ -1,4 +1,4 @@ -20241214: 560(todo),523(todo) +20241214: 560,523 20241208: 304,853,325 20241202: 370(todo),1109(todo) 20241130: 34,767 diff --git a/data/to_review.txt b/data/to_review.txt index ff532b9f..16290832 100644 --- a/data/to_review.txt +++ b/data/to_review.txt @@ -1,35 +1,35 @@ -2025-02-07 -> ['560(todo),523(todo)'] +2025-02-07 -> ['560,523'] 2025-02-01 -> ['304,853,325'] 2025-01-26 -> ['370(todo),1109(todo)'] 2025-01-24 -> ['34,767'] 2025-01-20 -> ['722,380'] 2025-01-19 -> ['33,81'] -2025-01-17 -> ['560(todo),523(todo)', '253'] +2025-01-17 -> ['560,523', '253'] 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,325', '394'] 2025-01-10 -> ['833,950'] 2025-01-05 -> ['370(todo),1109(todo)'] -2025-01-04 -> ['560(todo),523(todo)', '53,210,207'] +2025-01-04 -> ['560,523', '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,325', '33,81'] 2024-12-28 -> ['900'] -2024-12-27 -> ['560(todo),523(todo)', '253', '26,27', '802,1197,26'] +2024-12-27 -> ['560,523', '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-22 -> ['560(todo),523(todo)'] +2024-12-22 -> ['560,523'] 2024-12-21 -> ['304,853,325', '34,767', '394', '855,846'] 2024-12-20 -> ['833,950', '932'] -2024-12-19 -> ['560(todo),523(todo)'] +2024-12-19 -> ['560,523'] 2024-12-18 -> ['951,792'] -2024-12-17 -> ['560(todo),523(todo)', '722,380'] -2024-12-16 -> ['560(todo),523(todo)', '304,853,325', '33,81'] -2024-12-15 -> ['560(todo),523(todo)', '370(todo),1109(todo)'] +2024-12-17 -> ['560,523', '722,380'] +2024-12-16 -> ['560,523', '304,853,325', '33,81'] +2024-12-15 -> ['560,523', '370(todo),1109(todo)'] 2024-12-14 -> ['253', '53,210,207', '163,1048'] 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'] diff --git a/leetcode_java/src/main/java/LeetCodeJava/HashTable/SubarraySumEqualsK.java b/leetcode_java/src/main/java/LeetCodeJava/HashTable/SubarraySumEqualsK.java new file mode 100644 index 00000000..0ea5cb9c --- /dev/null +++ b/leetcode_java/src/main/java/LeetCodeJava/HashTable/SubarraySumEqualsK.java @@ -0,0 +1,151 @@ +package LeetCodeJava.HashTable; + +// https://leetcode.com/problems/subarray-sum-equals-k/description/ + +import java.util.HashMap; +import java.util.Map; + +/** + * 560. Subarray Sum Equals K + * Solved + * Medium + * Topics + * Companies + * Hint + * Given an array of integers nums and an integer k, return the total number of subarrays whose sum equals to k. + * + * A subarray is a contiguous non-empty sequence of elements within an array. + * + * + * + * Example 1: + * + * Input: nums = [1,1,1], k = 2 + * Output: 2 + * Example 2: + * + * Input: nums = [1,2,3], k = 3 + * Output: 2 + * + * + * Constraints: + * + * 1 <= nums.length <= 2 * 104 + * -1000 <= nums[i] <= 1000 + * -107 <= k <= 107 + * Seen this question in a real interview before? + * 1/5 + * Yes + * No + * Accepted + * 1.5M + * Submissions + * 3.4M + * Acceptance Rate + * 44.5% + * Topics + */ +public class SubarraySumEqualsK { + + // V0 + // IDEA : HASH MAP (fixed by gpt) + /** + * + * Explanation of Fixes: + * + * 1. Using presum - k Logic Properly: + * • If presum - k exists in the map, it means there is a subarray ending at the current index whose sum is k . Add the count of such prefix sums to the total. + * + * 2. Map Initialization: + * • Initializing the map with map.put(0, 1) allows the logic to handle cases where a subarray starting from index 0 equals k . + * + * 3. Updating the Map: + * • Instead of tracking indices, the map stores the count of occurrences of each prefix sum. This allows us to find how many subarrays can be formed using a specific prefix sum. + * + * 4. Simplifying Index-Based Logic: + * • 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 + Map map = new HashMap<>(); + int presum = 0; + int count = 0; + + // 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; + } + + // V1 + // IDEA : HASH MAP + // https://leetcode.com/problems/subarray-sum-equals-k/solutions/6143642/java-beats-9983-by-mohamedhazem3-9yj6/ + public static int subarraySum_1(int[] nums, int k) { + int[] prefix = new int[nums.length + 1]; + int out = 0; + HashMap hm = new HashMap<>(); + + for (int i = 0; i < nums.length; i++) + prefix[i + 1] = prefix[i] + nums[i]; // Compute prefix sums + + for (int i = 0; i < prefix.length; i++) { + if (hm.containsKey(prefix[i] - k)) // Check if required difference exists + out += hm.get(prefix[i] - k); // Add the frequency to the result + + // Update HashMap with the current prefix sum + hm.put(prefix[i], hm.getOrDefault(prefix[i], 0) + 1); + } + return out; + } + + // V2 + // IDEA : HASH MAP + // https://leetcode.com/problems/subarray-sum-equals-k/solutions/3167609/explained-beats-964-using-hashmap-in-jav-0b6o/ + public int subarraySum_2(int[] nums, int k) { + int sum = 0; + int ans = 0; + HashMap map = new HashMap<>(); + map.put(0,1); + for(int j=0;j map = new HashMap<>(); + int presum = 0; + int cnt = 0; + map.put(0, -1); // TODO : check if necessary + for (int i = 0; i < nums.length; i++){ + int cur = nums[i]; + presum += cur; + map.putIfAbsent(presum, i); // ??? + /** + * sum(i,j) = presum(j+1) - presum(i) + * + * k = presum(j+1) - presum(i) + * -> presum(i) = presum(j+1) - k + */ + if (map.containsKey(presum - k) && map.get(presum - k) == i+1){ + cnt += 1; + } + //map.putIfAbsent(presum, i); + } + + return cnt; + } + }