DeepRest

Contiguous Array

Feb 4th, 2022 (edited)
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.34 KB | None | 0 0
  1. class Solution:
  2.     def findMaxLength(self, nums: List[int]) -> int:
  3.         res = ps = 0
  4. #ps = no. of ones - no. of zeros
  5.         hm = {0: -1}
  6.         for i, e in enumerate(nums):
  7.             ps += 2*e - 1
  8.             if ps in hm:
  9.                 res = max(i-hm[ps], res)
  10.             else:
  11.                 hm[ps] = i
  12.         return res
Add Comment
Please, Sign In to add comment