Medium
Topics: Array, Sliding Window, Two Pointers
Given a binary array nums
, delete exactly one element.
Return the size of the longest non-empty subarray containing only 1
s in the resulting array.
Return 0
if there is no such subarray.
Example 1
Input: nums = [1,1,0,1]
Output: 3
Explanation: After deleting the element at index 2
(0-based), the array becomes [1,1,1]
, which has length 3
.
Example 2
Input: nums = [0,1,1,1,0,1,1,0,1]
Output: 5
Explanation: Delete the element at index 4
. One longest subarray of 1's is [1,1,1,1,1]
, length 5
.
Example 3
Input: nums = [1,1,1]
Output: 2
Explanation: You must delete one element, so the best you can get is length 2
.
1 <= nums.length <= 10^5
nums[i]
is either 0
or 1
https://leetcode.com/problems/longest-subarray-of-1s-after-deleting-one-element/
Loading component...
Loading component...