Longest subarray of 1s after deleting one element
November 29, 2022
two-pointersProblem URL: Longest subarray of 1s after deleting one element
We will use two pointers to keep track of the start and end of the subarray. We will use a variable to keep track of the number of zeros in the subarray. If the number of zeros is less than or equal to 1, we will increment the end pointer. If the number of zeros is greater than 1, we will increment the start pointer. We will update the maximum length of the subarray at each step.
class Solution:
def longestSubarray(self, nums: List[int]) -> int:
k, i = 1, 0
for j in range(len(nums)):
k -= nums[j] == 0
if k < 0:
k += nums[i] == 0
i += 1
return j-i
Time complexity: O(n)
, n is the length of the array
Space complexity: O(1)