Talentd
FleetCode

525. Contiguous Array

Medium52.2% acceptance16 companies

Given a binary array nums, return the maximum length of a contiguous subarray with an equal number of 0 and 1.

Example 1:

Input: nums = [0,1]
Output: 2
Explanation: [0, 1] is the longest contiguous subarray with an equal number of 0 and 1.

Example 2:

Input: nums = [0,1,0]
Output: 2
Explanation: [0, 1] (or [1, 0]) is a longest contiguous subarray with equal number of 0 and 1.

Constraints:

  • 1 <= nums.length <= 105
  • nums[i] is either 0 or 1.
13:12
Still stuck?Read 3 approaches, or watch a walkthrough.
Open solution
Loading editor…
1class Solution:
2 def findMaxLength(self, nums: List[int]) -> int:
3
Starter codePython 3Spaces: 4
Examples
Input
[0,1]
Input
[0,1,0]
Input
[0,1,1,1,1,1,0,0,0]
Your code is saved on this device.Solution