-
Notifications
You must be signed in to change notification settings - Fork 44
/
maximum-size-subarray-sum-equals-k.py
167 lines (141 loc) · 5.21 KB
/
maximum-size-subarray-sum-equals-k.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
"""
325. Maximum Size Subarray Sum Equals k
Medium
Given an integer array nums and an integer k, return the maximum length of a subarray that sums to k. If there is not one, return 0 instead.
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.
Constraints:
1 <= nums.length <= 2 * 105
-104 <= nums[i] <= 104
-109 <= k <= 109
"""
# V0
# time complexity : O(N) | space complexity : O(N)
# IDEA : HASH TBALE
# -> have a var acc keep sum of all item in nums,
# -> and use dic collect acc and its index
# -> since we want to find nums[i:j] = k -> so it's a 2 sum problem now
# -> i.e. if acc - k in dic => there must be a solution (i,j) of nums[i:j] = k
# -> return the max result
# -> ### acc DEMO : given array a = [1,2,3,4,5] ###
# -> acc_list = [1,3,6,10,15]
# -> so sum(a[1:3]) = 9 = acc_list[3] - acc_list[1-1] = 10 - 1 = 9
class Solution(object):
def maxSubArrayLen(self, nums, k):
result, acc = 0, 0
# NOTE !!! we init dic as {0:-1} ({sum:idx})
dic = {0: -1}
for i in range(len(nums)):
acc += nums[i]
if acc not in dic:
### NOTE : we save idx as dict value
dic[acc] = i
### acc - x = k -> so x = acc - k, that's why we check if acc - x in the dic or not
if acc - k in dic:
result = max(result, i - dic[acc-k])
return result
# V0'
# IDEA : BRUTE FORCE
# time complexity : O(N^2) | space complexity : O(N)
class Solution(object):
def maxSubArrayLen(self, nums, k):
# O(n^2)
tmp = []
for i in range(len(nums)):
for j in range(i, len(nums)):
if sum(nums[i:j]) == k:
tmp.append(j-i)
return 0 if len(tmp) == 0 else max(tmp)
# V1
# IDEA : Prefix Sum + Hash Map
# https://leetcode.com/problems/maximum-size-subarray-sum-equals-k/solution/
class Solution:
def maxSubArrayLen(self, nums: List[int], k: int) -> int:
prefix_sum = longest_subarray = 0
indices = {}
for i, num in enumerate(nums):
prefix_sum += num
# Check if all of the numbers seen so far sum to k.
if prefix_sum == k:
longest_subarray = i + 1
# If any subarray seen so far sums to k, then
# update the length of the longest_subarray.
if prefix_sum - k in indices:
longest_subarray = max(longest_subarray, i - indices[prefix_sum - k])
# Only add the current prefix_sum index pair to the
# map if the prefix_sum is not already in the map.
if prefix_sum not in indices:
indices[prefix_sum] = i
return longest_subarray
# V1'
# http://www.voidcn.com/article/p-pbvzylrp-qp.html
# https://www.cnblogs.com/lightwindy/p/9760070.html
# IDEA : DICT
# STEPS :
# -> HAVE A dic RECORD THE SUMS AND THEIR INDEX
# -> GO THROUGH ALL ELEMENTS IN nums AND KEEP DOING ACCUMULATED SUM : acc
# -> IF acc not in dict : dic[acc] = i (add sum and it index to dict)
# -> IF acc - k in dic : result = max(result, i - dic[acc-k])
# acc + sth = k ---> acc - k = sth, so if acc - k already in dict ---> acc + sth = k MUST EXIST
# SO IF ABOVE CASE EXIST, THEN WE DO max(result, i - dic[acc-k]) : TO RETURN THE MAX LENGTH OF SUB ARRAY
# i - dic[acc-k] : GET THE LENGTH BETWEEN i and sth (sth = dic[acc-k] )
class Solution(object):
def maxSubArrayLen(self, nums, k):
result, acc = 0, 0
dic = {0: -1}
for i in range(len(nums)):
acc += nums[i]
if acc not in dic:
dic[acc] = i
if acc - k in dic:
result = max(result, i - dic[acc-k])
return result
# V1''
# https://www.jiuzhang.com/solution/maximum-size-subarray-sum-equals-k/#tag-highlight-lang-python
class Solution:
"""
@param nums: an array
@param k: a target value
@return: the maximum length of a subarray that sums to k
"""
def maxSubArrayLen(self, nums, k):
# Write your code here
m = {}
ans = 0
m[k] = 0
n = len(nums)
sum = [0 for i in range(n + 1)]
for i in range(1, n + 1):
sum[i] = sum[i - 1] + nums[i - 1] # use this trick to "record" accumulated sum, avoid using double loop
if sum[i] in m:
ans = max(ans, i - m[sum[i]])
if sum[i] + k not in m:
m[sum[i] + k] = i
return ans
# V2
# Time: O(n)
# Space: O(n)
class Solution(object):
def maxSubArrayLen(self, nums, k):
"""
:type nums: List[int]
:type k: int
:rtype: int
"""
sums = {}
cur_sum, max_len = 0, 0
for i in range(len(nums)):
cur_sum += nums[i]
if cur_sum == k:
max_len = i + 1
elif cur_sum - k in sums:
max_len = max(max_len, i - sums[cur_sum - k])
if cur_sum not in sums:
sums[cur_sum] = i # Only keep the smallest index.
return max_len