Skip to content

Commit

Permalink
update 325 java, cheatsheet
Browse files Browse the repository at this point in the history
  • Loading branch information
yennanliu committed Dec 8, 2024
1 parent d8af5e7 commit 1820466
Show file tree
Hide file tree
Showing 3 changed files with 172 additions and 1 deletion.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -354,7 +354,7 @@
290| [Word Pattern](https://leetcode.com/problems/word-pattern/) | [Python ](./leetcode_python/Hash_table/word-pattern.py) | _O(n)_ | _O(c)_ | Easy | `basic`, chcek `# 205 Isomorphic Strings`,`good basic`, `for ptn, word in zip(pattern, words)`, `dropbox` , `UBER`| AGAIN* (3)
299| [Bulls and Cows](https://leetcode.com/problems/bulls-and-cows/) | [Python ](./leetcode_python/Hash_table/bulls-and-cows.py) | _O(n)_ | _O(1)_ | Easy |`trick`,`map(operator.eq, a, b)`,`amazon`, `airbnb`, `google`| AGAIN* (3)
314| [Binary Tree Vertical Order Traversal](https://leetcode.com/problems/binary-tree-vertical-order-traversal/) | [Python ](./leetcode_python/Hash_table/binary-tree-vertical-order-traversal.py) | _O(n)_ | _O(n)_| Medium | 🔒, `BFS`,`DFS`,`hash table`, `tree`, `trick`,`google`, `amazon`,`fb`, m$ | OK**** (6)
325| [Maximum Size Subarray Sum Equals k](https://leetcode.com/problems/maximum-size-subarray-sum-equals-k/) | [Python](./leetcode_python/Hash_table/maximum-size-subarray-sum-equals-k.py), [Java](./leetcode_java/src/main/java/LeetCodeJava/HashTable/MaximumSizeSubarraySumEqualsK.java) | _O(n)_ | _O(n)_| Medium | prefix sum, 🔒, `basic`, `good trick`, `dict`, `fb`| AGAIN************** (8) (MUST)
325| [Maximum Size Subarray Sum Equals k](https://leetcode.com/problems/maximum-size-subarray-sum-equals-k/) | [Python](./leetcode_python/Hash_table/maximum-size-subarray-sum-equals-k.py), [Java](./leetcode_java/src/main/java/LeetCodeJava/HashTable/MaximumSizeSubarraySumEqualsK.java) | _O(n)_ | _O(n)_| Medium | prefix sum, 🔒, `hashmap`, `good trick`, `dict`, `fb`| AGAIN***************** (8) (MUST)
356| [Line Reflection](https://leetcode.com/problems/line-reflection/) | [Python ](./leetcode_python/Hash_table/line-reflection.py) | _O(n)_| _O(n)_| Medium |🔒, Hash, Two Pointers,`math`,`google`| AGAIN (not start)
359| [Logger Rate Limiter](https://leetcode.com/problems/logger-rate-limiter/) | [Python](./leetcode_python/Hash_table/logger_rate_lmiter.py.py), [Java](./leetcode_java/src/main/java/LeetCodeJava/HashTable/LoggerRateLimiter.java) | _O(1), amortized_ | _O(k)_ | Easy |🔒, google| OK |
387| [First Unique Character in a String](https://leetcode.com/problems/first-unique-character-in-a-string/) | [Python ](./leetcode_python/Hash_table/first-unique-character-in-a-string.py) | _O(n)_| _O(n)_| Easy |`amazon`, `apple`, `fb`| OK
Expand Down
70 changes: 70 additions & 0 deletions doc/cheatsheet/hash_map.md
Original file line number Diff line number Diff line change
Expand Up @@ -916,4 +916,74 @@ class Solution(object):
_counter.update([cum_sum])
count = max(count, _counter[cum_sum])
return len(wall) - count
```

### 2-13) Maximum Size Subarray Sum Equals k

```java
// java
// LC 325
public int maxSubArrayLen_0_1(int[] nums, int k) {
// Map to store (prefixSum, index)
Map<Integer, Integer> preSumMap = new HashMap<>();
preSumMap.put(0, -1); // Initialize for subarrays starting from index 0

int curSum = 0;
int maxSize = 0;

for (int i = 0; i < nums.length; i++) {
curSum += nums[i];

// Check if there's a prefix sum such that curSum - prefixSum = k
/**
* Prefix sum
*
*
* The prefix sum approach works because any subarray sum can be expressed in terms of two prefix sums:
*
*
* sum of subarray[i,j] = prefixSum[j] - prefixSum[i-1]
*
*
* Where:
* • prefixSum[j] is the cumulative sum of the array up to index j.
* • prefixSum[i-1] is the cumulative sum of the array up to index i-1.
*
* Rewriting this:
*
* -> prefixSum[j] - prefixSum[i-1] = k
*
* -> prefixSum[i-1] = prefixSum[j] - k
*
*
* Thus, the task is to find a previous prefix
* sum (prefixSum[i-1]) such that the
* difference between the current
* prefix sum (prefixSum[j]) and that value equals k.
*
*
*
* How the Code Works
*
* 1. Tracking Prefix Sums:
* • curSum is the cumulative prefix sum up to the current index i.
* • The map preSumMap stores previously seen prefix sums as keys, with their earliest index as the value.
* 2. Checking for Subarrays:
* • At any index i, the condition curSum - k checks if there exists a previously seen prefix sum that, when subtracted from the current cumulative sum, gives the desired subarray sum k.
*
* 3. Why It Covers All Possible Subarrays******:
* • The map contains all prefix sums seen so far, so it inherently includes all potential starting points of subarrays.
* • If a subarray [start, i] has a sum equal to k, the difference curSum - k corresponds to the prefix sum at start - 1. Since the map stores all previously seen prefix sums, this difference is guaranteed to be checked.
*
*/
if (preSumMap.containsKey(curSum - k)) {
maxSize = Math.max(maxSize, i - preSumMap.get(curSum - k));
}

// Add current prefix sum to the map if not already present
preSumMap.putIfAbsent(curSum, i);
}

return maxSize;
}
```
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,107 @@ public class MaximumSizeSubarraySumEqualsK {
// V0
// TODO : implement

// V0-1
// IDEA : HASHMAP (gpt)
/**
* Examples:
*
* int[] nums = {1, 2, -1, 3};
* int k = 3;
*
*
* 1. Initialization:
* • preSumMap: Initially {0: -1} (to handle subarrays starting at index 0).
*
* 2. Iteration:
* • Index 0:
* • curSum = 1
* • curSum - k = 1 - 3 = -2 (not in map)
* • Update map: {0: -1, 1: 0}
*
* • Index 1:
* • curSum = 3
* • curSum - k = 3 - 3 = 0 (in map at index -1)
* • Subarray [0, 1] has sum 3, so maxSize = max(0, 1 - (-1)) = 2
* • Update map: {0: -1, 1: 0, 3: 1}
*
*
* • Index 2:
* • curSum = 2
* • curSum - k = 2 - 3 = -1 (not in map)
* • Update map: {0: -1, 1: 0, 3: 1, 2: 2}
*
*
* • Index 3:
* • curSum = 5
* • curSum - k = 5 - 3 = 2 (in map at index 2)
* • Subarray [3, 3] has sum 3, so maxSize = max(2, 3 - 2) = 2
* • Update map: {0: -1, 1: 0, 3: 1, 2: 2, 5: 3}
*/
public int maxSubArrayLen_0_1(int[] nums, int k) {
// Map to store (prefixSum, index)
Map<Integer, Integer> preSumMap = new HashMap<>();
preSumMap.put(0, -1); // Initialize for subarrays starting from index 0

int curSum = 0;
int maxSize = 0;

for (int i = 0; i < nums.length; i++) {
curSum += nums[i];

// Check if there's a prefix sum such that curSum - prefixSum = k
/**
* Prefix sum
*
*
* The prefix sum approach works because any subarray sum can be expressed in terms of two prefix sums:
*
*
* sum of subarray[i,j] = prefixSum[j] - prefixSum[i-1]
*
*
* Where:
* • prefixSum[j] is the cumulative sum of the array up to index j.
* • prefixSum[i-1] is the cumulative sum of the array up to index i-1.
*
* Rewriting this:
*
* -> prefixSum[j] - prefixSum[i-1] = k
*
* -> prefixSum[i-1] = prefixSum[j] - k
*
*
* Thus, the task is to find a previous prefix
* sum (prefixSum[i-1]) such that the
* difference between the current
* prefix sum (prefixSum[j]) and that value equals k.
*
*
*
* How the Code Works
*
* 1. Tracking Prefix Sums:
* • curSum is the cumulative prefix sum up to the current index i.
* • The map preSumMap stores previously seen prefix sums as keys, with their earliest index as the value.
* 2. Checking for Subarrays:
* • At any index i, the condition curSum - k checks if there exists a previously seen prefix sum that, when subtracted from the current cumulative sum, gives the desired subarray sum k.
*
* 3. Why It Covers All Possible Subarrays******:
* • The map contains all prefix sums seen so far, so it inherently includes all potential starting points of subarrays.
* • If a subarray [start, i] has a sum equal to k, the difference curSum - k corresponds to the prefix sum at start - 1. Since the map stores all previously seen prefix sums, this difference is guaranteed to be checked.
*
*/
if (preSumMap.containsKey(curSum - k)) {
maxSize = Math.max(maxSize, i - preSumMap.get(curSum - k));
}

// Add current prefix sum to the map if not already present
preSumMap.putIfAbsent(curSum, i);
}

return maxSize;
}

// V1
// https://leetcode.com/problems/maximum-size-subarray-sum-equals-k/solutions/1017059/java-prefix-sums/
public int maxSubArrayLen(int[] nums, int k) {
Expand Down

0 comments on commit 1820466

Please sign in to comment.