Short n Easy Python


  • 0
    E

    Similar usage of hash table as two sum: keep track of count diff between 0's and 1's and see if you can offset the current difference with a sum earlier in the array. Another key point is to store only the earliest of unique diffs as we want to maximize the length of the subarray.

        def findMaxLength(self, nums):
            """
            :type nums: List[int]
            :rtype: int
            """
            if not nums: return 0
            hashT, res, diff = {0:-1}, 0, 0
            for i,n in enumerate(nums):
                diff += 1 if n == 1 else -1
                if diff in hashT:
                    res = max(res, i-hashT[diff])
                else:
                    hashT[diff] = i
            return res   
    

Log in to reply
 

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