From d8af5e77c3c7ced626a01c99bc25d07959c24e66 Mon Sep 17 00:00:00 2001 From: yennanliu Date: Sun, 8 Dec 2024 17:03:05 +0800 Subject: [PATCH] update 325 java --- data/progress.txt | 2 +- data/to_review.txt | 18 ++--- .../MaximumSizeSubarraySumEqualsK.java | 45 +++++++++++++ .../src/main/java/dev/workspace5.java | 66 +++++++++++++++++++ 4 files changed, 121 insertions(+), 10 deletions(-) diff --git a/data/progress.txt b/data/progress.txt index dd5a7eea..eb66763c 100644 --- a/data/progress.txt +++ b/data/progress.txt @@ -1,4 +1,4 @@ -20241208: 304,853 +20241208: 304,853,325 20241202: 370(todo),1109(todo) 20241130: 34,767 20241126: 722,380 diff --git a/data/to_review.txt b/data/to_review.txt index b6cc16da..523f56ee 100644 --- a/data/to_review.txt +++ b/data/to_review.txt @@ -1,4 +1,4 @@ -2025-02-01 -> ['304,853'] +2025-02-01 -> ['304,853,325'] 2025-01-26 -> ['370(todo),1109(todo)'] 2025-01-24 -> ['34,767'] 2025-01-20 -> ['722,380'] @@ -7,32 +7,32 @@ 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', '394'] +2025-01-11 -> ['304,853,325', '394'] 2025-01-10 -> ['833,950'] 2025-01-05 -> ['370(todo),1109(todo)'] 2025-01-04 -> ['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', '33,81'] +2024-12-29 -> ['304,853,325', '33,81'] 2024-12-28 -> ['900'] 2024-12-27 -> ['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-21 -> ['304,853', '34,767', '394', '855,846'] +2024-12-21 -> ['304,853,325', '34,767', '394', '855,846'] 2024-12-20 -> ['833,950', '932'] 2024-12-18 -> ['951,792'] 2024-12-17 -> ['722,380'] -2024-12-16 -> ['304,853', '33,81'] +2024-12-16 -> ['304,853,325', '33,81'] 2024-12-15 -> ['370(todo),1109(todo)'] 2024-12-14 -> ['253', '53,210,207', '163,1048'] -2024-12-13 -> ['304,853', '34,767', '776,31', '444', '298,729'] +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'] -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)'] -2024-12-10 -> ['304,853', '370(todo),1109(todo)'] -2024-12-09 -> ['304,853', '722,380'] +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)'] +2024-12-10 -> ['304,853,325', '370(todo),1109(todo)'] +2024-12-09 -> ['304,853,325', '722,380'] 2024-12-08 -> ['34,767', '33,81', '394', '737'] 2024-12-07 -> ['370(todo),1109(todo)', '833,950', '900', '686,734,737'] 2024-12-06 -> ['253', '26,27', '802,1197,26', '353'] diff --git a/leetcode_java/src/main/java/LeetCodeJava/HashTable/MaximumSizeSubarraySumEqualsK.java b/leetcode_java/src/main/java/LeetCodeJava/HashTable/MaximumSizeSubarraySumEqualsK.java index fcde714b..4d37bc24 100644 --- a/leetcode_java/src/main/java/LeetCodeJava/HashTable/MaximumSizeSubarraySumEqualsK.java +++ b/leetcode_java/src/main/java/LeetCodeJava/HashTable/MaximumSizeSubarraySumEqualsK.java @@ -7,9 +7,39 @@ import java.util.HashMap; import java.util.Map; +/** + * 325. Maximum Size Subarray Sum Equals k + * 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. + * + * Note: + * The sum of the entire nums array is guaranteed to fit within the 32-bit signed integer range. + * + * Example 1: + * + * Input: nums = [1, -1, 5, -2, 3], k = 3 + * Output: 4 + * Explanation: The subarray [1, -1, 5, -2] sums to 3 and is the longest. + * Example 2: + * + * Input: nums = [-2, -1, 2, 1], k = 1 + * Output: 2 + * Explanation: The subarray [-1, 2] sums to 1 and is the longest. + * Follow Up: + * Can you do it in O(n) time? + * + * Difficulty: + * Medium + * Lock: + * Prime + * Company: + * Amazon Facebook Google Palantir Technologies + * + * + */ public class MaximumSizeSubarraySumEqualsK { // V0 + // TODO : implement // V1 // 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) { return max; } + // V4 + // https://leetcode.ca/2016-10-20-325-Maximum-Size-Subarray-Sum-Equals-k/ + public int maxSubArrayLen_4(int[] nums, int k) { + Map d = new HashMap<>(); + d.put(0L, -1); + int ans = 0; + long s = 0; + for (int i = 0; i < nums.length; ++i) { + s += nums[i]; + ans = Math.max(ans, i - d.getOrDefault(s - k, i)); + d.putIfAbsent(s, i); + } + return ans; + } + } diff --git a/leetcode_java/src/main/java/dev/workspace5.java b/leetcode_java/src/main/java/dev/workspace5.java index 2c471196..845271dc 100644 --- a/leetcode_java/src/main/java/dev/workspace5.java +++ b/leetcode_java/src/main/java/dev/workspace5.java @@ -4338,6 +4338,72 @@ public int carFleet(int target, int[] position, int[] speed){ return stack.size(); } + // LC 325 + // https://leetcode.ca/all/325.html + // https://leetcode.ca/2016-10-20-325-Maximum-Size-Subarray-Sum-Equals-k/ + // 4.22 - 4.45 pm + /** + * 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. + * + * + * Idea : hashmap, presum array + * + * exp 1) + * nums = [1, -1, 5, -2, 3], k = 3 + * + * -> presum = [0,1,0,5,3,6] !!!!! + * -> sum(i,j) = presum[j+1] - presum[i] + * -> sum(0,3) = presum[4] = presum[0] = 3 - 0 = 3 + */ + public int maxSubArrayLen(int[] nums, int k) { + + // V1: presum array +// Integer[] preSumArray = new Integer[nums.length+1]; +// int curSum = 0; +// preSumArray[0] = 0; +// for(int i = 1; i < nums.length; i++){ +// curSum += nums[i]; +// preSumArray[i] = curSum; +// } + + // V2 : hash map + // map : (preSum, idx) + Map preSumMap = new HashMap<>(); + int curSum_ = 0; + for(int i = 1; i < nums.length; i++){ + curSum_ += nums[i]; + //preSumMap.putIfAbsent(nums[i], curSum_); + preSumMap.putIfAbsent(curSum_, i); + } + + + int maxSize = 0; + // TODO : optimize double loop +// for(int i = 0; i < preSumArray.length-1; i++){ +// for (int j = i+1; j < preSumArray.length; j++){ +// int subArraySum = preSumArray[j+1] - preSumArray[i]; +// if (subArraySum == k){ +// maxSize = Math.max(maxSize, j-i+1); +// } +// } +// } + + // Optimized : use hashmap, check if "k-x" is in hashmap (similar as "2 sum") + for (int i = 0; i < nums.length; i++){ + int val = nums[i]; + // x + nums[i] = k + // -> x = k - nums[i] + if (preSumMap.containsKey(k - val)){ + maxSize = Math.max(maxSize, (preSumMap.get(k-val) - i + 1)); + } + } + + return maxSize; + } + + }