Skip to content

Commit 1820466

Browse files
committed
update 325 java, cheatsheet
1 parent d8af5e7 commit 1820466

File tree

3 files changed

+172
-1
lines changed

3 files changed

+172
-1
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -354,7 +354,7 @@
354354
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)
355355
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)
356356
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)
357-
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)
357+
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)
358358
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)
359359
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 |
360360
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

doc/cheatsheet/hash_map.md

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -916,4 +916,74 @@ class Solution(object):
916916
_counter.update([cum_sum])
917917
count = max(count, _counter[cum_sum])
918918
return len(wall) - count
919+
```
920+
921+
### 2-13) Maximum Size Subarray Sum Equals k
922+
923+
```java
924+
// java
925+
// LC 325
926+
public int maxSubArrayLen_0_1(int[] nums, int k) {
927+
// Map to store (prefixSum, index)
928+
Map<Integer, Integer> preSumMap = new HashMap<>();
929+
preSumMap.put(0, -1); // Initialize for subarrays starting from index 0
930+
931+
int curSum = 0;
932+
int maxSize = 0;
933+
934+
for (int i = 0; i < nums.length; i++) {
935+
curSum += nums[i];
936+
937+
// Check if there's a prefix sum such that curSum - prefixSum = k
938+
/**
939+
* Prefix sum
940+
*
941+
*
942+
* The prefix sum approach works because any subarray sum can be expressed in terms of two prefix sums:
943+
*
944+
*
945+
* sum of subarray[i,j] = prefixSum[j] - prefixSum[i-1]
946+
*
947+
*
948+
* Where:
949+
* • prefixSum[j] is the cumulative sum of the array up to index j.
950+
* • prefixSum[i-1] is the cumulative sum of the array up to index i-1.
951+
*
952+
* Rewriting this:
953+
*
954+
* -> prefixSum[j] - prefixSum[i-1] = k
955+
*
956+
* -> prefixSum[i-1] = prefixSum[j] - k
957+
*
958+
*
959+
* Thus, the task is to find a previous prefix
960+
* sum (prefixSum[i-1]) such that the
961+
* difference between the current
962+
* prefix sum (prefixSum[j]) and that value equals k.
963+
*
964+
*
965+
*
966+
* How the Code Works
967+
*
968+
* 1. Tracking Prefix Sums:
969+
* • curSum is the cumulative prefix sum up to the current index i.
970+
* • The map preSumMap stores previously seen prefix sums as keys, with their earliest index as the value.
971+
* 2. Checking for Subarrays:
972+
* • 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.
973+
*
974+
* 3. Why It Covers All Possible Subarrays******:
975+
* • The map contains all prefix sums seen so far, so it inherently includes all potential starting points of subarrays.
976+
* • 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.
977+
*
978+
*/
979+
if (preSumMap.containsKey(curSum - k)) {
980+
maxSize = Math.max(maxSize, i - preSumMap.get(curSum - k));
981+
}
982+
983+
// Add current prefix sum to the map if not already present
984+
preSumMap.putIfAbsent(curSum, i);
985+
}
986+
987+
return maxSize;
988+
}
919989
```

leetcode_java/src/main/java/LeetCodeJava/HashTable/MaximumSizeSubarraySumEqualsK.java

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,107 @@ public class MaximumSizeSubarraySumEqualsK {
4141
// V0
4242
// TODO : implement
4343

44+
// V0-1
45+
// IDEA : HASHMAP (gpt)
46+
/**
47+
* Examples:
48+
*
49+
* int[] nums = {1, 2, -1, 3};
50+
* int k = 3;
51+
*
52+
*
53+
* 1. Initialization:
54+
* • preSumMap: Initially {0: -1} (to handle subarrays starting at index 0).
55+
*
56+
* 2. Iteration:
57+
* • Index 0:
58+
* • curSum = 1
59+
* • curSum - k = 1 - 3 = -2 (not in map)
60+
* • Update map: {0: -1, 1: 0}
61+
*
62+
* • Index 1:
63+
* • curSum = 3
64+
* • curSum - k = 3 - 3 = 0 (in map at index -1)
65+
* • Subarray [0, 1] has sum 3, so maxSize = max(0, 1 - (-1)) = 2
66+
* • Update map: {0: -1, 1: 0, 3: 1}
67+
*
68+
*
69+
* • Index 2:
70+
* • curSum = 2
71+
* • curSum - k = 2 - 3 = -1 (not in map)
72+
* • Update map: {0: -1, 1: 0, 3: 1, 2: 2}
73+
*
74+
*
75+
* • Index 3:
76+
* • curSum = 5
77+
* • curSum - k = 5 - 3 = 2 (in map at index 2)
78+
* • Subarray [3, 3] has sum 3, so maxSize = max(2, 3 - 2) = 2
79+
* • Update map: {0: -1, 1: 0, 3: 1, 2: 2, 5: 3}
80+
*/
81+
public int maxSubArrayLen_0_1(int[] nums, int k) {
82+
// Map to store (prefixSum, index)
83+
Map<Integer, Integer> preSumMap = new HashMap<>();
84+
preSumMap.put(0, -1); // Initialize for subarrays starting from index 0
85+
86+
int curSum = 0;
87+
int maxSize = 0;
88+
89+
for (int i = 0; i < nums.length; i++) {
90+
curSum += nums[i];
91+
92+
// Check if there's a prefix sum such that curSum - prefixSum = k
93+
/**
94+
* Prefix sum
95+
*
96+
*
97+
* The prefix sum approach works because any subarray sum can be expressed in terms of two prefix sums:
98+
*
99+
*
100+
* sum of subarray[i,j] = prefixSum[j] - prefixSum[i-1]
101+
*
102+
*
103+
* Where:
104+
* • prefixSum[j] is the cumulative sum of the array up to index j.
105+
* • prefixSum[i-1] is the cumulative sum of the array up to index i-1.
106+
*
107+
* Rewriting this:
108+
*
109+
* -> prefixSum[j] - prefixSum[i-1] = k
110+
*
111+
* -> prefixSum[i-1] = prefixSum[j] - k
112+
*
113+
*
114+
* Thus, the task is to find a previous prefix
115+
* sum (prefixSum[i-1]) such that the
116+
* difference between the current
117+
* prefix sum (prefixSum[j]) and that value equals k.
118+
*
119+
*
120+
*
121+
* How the Code Works
122+
*
123+
* 1. Tracking Prefix Sums:
124+
* • curSum is the cumulative prefix sum up to the current index i.
125+
* • The map preSumMap stores previously seen prefix sums as keys, with their earliest index as the value.
126+
* 2. Checking for Subarrays:
127+
* • 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.
128+
*
129+
* 3. Why It Covers All Possible Subarrays******:
130+
* • The map contains all prefix sums seen so far, so it inherently includes all potential starting points of subarrays.
131+
* • 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.
132+
*
133+
*/
134+
if (preSumMap.containsKey(curSum - k)) {
135+
maxSize = Math.max(maxSize, i - preSumMap.get(curSum - k));
136+
}
137+
138+
// Add current prefix sum to the map if not already present
139+
preSumMap.putIfAbsent(curSum, i);
140+
}
141+
142+
return maxSize;
143+
}
144+
44145
// V1
45146
// https://leetcode.com/problems/maximum-size-subarray-sum-equals-k/solutions/1017059/java-prefix-sums/
46147
public int maxSubArrayLen(int[] nums, int k) {

0 commit comments

Comments
 (0)