My LeetCode grinding. Trying to do a problem a day.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

123456789101112131415161718192021222324252627282930
  1. class Solution:
  2. def findMaxLength(self, nums):
  3. result = []
  4. current = 0
  5. for n in nums:
  6. if n == 0:
  7. current -= 1
  8. else:
  9. current += 1
  10. result.append(current)
  11. hm = dict()
  12. hm[0] = [-1, None]
  13. for ind, val in enumerate(result):
  14. if val in hm:
  15. hm[val][1] = ind
  16. else:
  17. hm[val] = [ind, None]
  18. maxLength = 0
  19. for key, value in hm.items():
  20. if value[1] is not None:
  21. new = value[1] - value[0]
  22. if new > maxLength:
  23. maxLength = new
  24. return maxLength
  25. s = Solution()
  26. print("Expected: 2")
  27. print("Got:", s.findMaxLength([1, 0]))