O(n) Ruby solution


  • 0
    T
    def max_sub_array_len(nums, k)
      sums = {}
      cur_sum = 0
      max_len = 0
      sums[0] = -1
      (0...nums.length).each do |i|
        cur_sum += nums[i]
        sums[cur_sum] ||= i
        j = sums[cur_sum - k]
        max_len = [i - j, max_len].max if j
      end
      return max_len
    end
    

Log in to reply
 

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