Skip to content

Commit fe151c8

Browse files
committed
Runtime: 631 ms (Top 77.84%) | Memory: 30.70 MB (Top 96.45%)
1 parent 374d7c2 commit fe151c8

File tree

1 file changed

+15
-28
lines changed

1 file changed

+15
-28
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,19 @@
1-
class Solution(object):
2-
def maximumSubarraySum(self, nums, k):
3-
"""
4-
:type nums: List[int]
5-
:type k: int
6-
:rtype: int
7-
"""
8-
#number mapped to idx... curr subarray ke mappings honge to check repeititions
9-
maps = {}
10-
ans = 0
1+
// Runtime: 631 ms (Top 77.84%) | Memory: 30.70 MB (Top 96.45%)
2+
3+
class Solution:
4+
def maximumSubarraySum(self, nums: List[int], k: int) -> int:
115

12-
l, r = 0, 0
13-
curr = 0
6+
res, cur, pos, dup = 0, 0, [-1] * 100001, -1
147

15-
while r < len(nums):
8+
for i in range(0,len(nums)):
9+
cur += nums[i] # compute running sum for
10+
if i >= k: cur -= nums[i-k] # the window of length k
1611

17-
while l < r and (len(maps) >= k or nums[r] in maps):
18-
curr -= nums[l]
19-
maps.pop(nums[l])
20-
l += 1
12+
dup = max(dup, pos[nums[i]]) # update LAST seen duplicate
2113

22-
curr += nums[r]
23-
maps[nums[r]] = r
24-
25-
if len(maps) == k:
26-
ans = max(curr, ans)
27-
28-
r += 1
29-
30-
31-
32-
return ans
14+
if i - dup >= k: # if no duplicates were found
15+
res = max(res, cur) # update max window sum
16+
17+
pos[nums[i]] = i
18+
19+
return res

0 commit comments

Comments
 (0)