Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
from typing import List

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
Loading