Contiguous Array - LeetCode
Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared…
Given a binary array
nums, return the maximum length of a contiguous subarray with an equal number of
Input: nums = [0,1]
Explanation: [0, 1] is the longest contiguous subarray with an equal number of 0 and 1.
Input: nums = [0,1,0]
Explanation: [0, 1] (or [1, 0]) is a longest contiguous subarray with equal number of 0 and 1.
1 <= nums.length <= 105
- I was think using sliding window in this question, and hashmap would be good to notice current total sum.
- if we found another number with the same sum, which mean they have the same 0 and 1 between them. calculate the different between these two number
3. map.put(0, -1) -> current sum equal 0, add the first one if total array equal to 0.