You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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
Copy file name to clipboardExpand all lines: doc/cheatsheet/hash_map.md
+70Lines changed: 70 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -916,4 +916,74 @@ class Solution(object):
916
916
_counter.update([cum_sum])
917
917
count =max(count, _counter[cum_sum])
918
918
returnlen(wall) - count
919
+
```
920
+
921
+
### 2-13) Maximum Size Subarray Sum Equals k
922
+
923
+
```java
924
+
// java
925
+
// LC 325
926
+
publicint maxSubArrayLen_0_1(int[] nums, int k) {
927
+
// Map to store (prefixSum, index)
928
+
Map<Integer, Integer> preSumMap =newHashMap<>();
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
Copy file name to clipboardExpand all lines: leetcode_java/src/main/java/LeetCodeJava/HashTable/MaximumSizeSubarraySumEqualsK.java
+101Lines changed: 101 additions & 0 deletions
Original file line number
Diff line number
Diff line change
@@ -41,6 +41,107 @@ public class MaximumSizeSubarraySumEqualsK {
41
41
// V0
42
42
// TODO : implement
43
43
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
+
publicintmaxSubArrayLen_0_1(int[] nums, intk) {
82
+
// Map to store (prefixSum, index)
83
+
Map<Integer, Integer> preSumMap = newHashMap<>();
84
+
preSumMap.put(0, -1); // Initialize for subarrays starting from index 0
85
+
86
+
intcurSum = 0;
87
+
intmaxSize = 0;
88
+
89
+
for (inti = 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
0 commit comments