6 lines in Python, one pass


  • 1
    class Solution(object):
        def maxSubArrayLen(self, nums, k):
            last, sums, d, res = 0, 0, {0: 0}, 0
            for i, num in enumerate(nums):
                sums += num + last
                if sums - k in d: res = max(res, i - d[sums - k] + 1)
                if sums not in d: d[sums] = i + 1
            return res
    
        # 35 / 35 test cases passed.
        # Status: Accepted
        # Runtime: 84 ms
    
    

  • 0

    Nice code. But what is the point of variable last here?


  • 0

    clean code, but not friendly


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.