1493. Longest Subarray of 1’s After Deleting One Element
The problem asks us to find the longest subarray that consists of only 1’s - but the twist is that we are allowed to delete any singular 0 that might be stopping us from finding a longest subarray.
We keep track of a two things in this problem - first, the longest subarray consisting of 1’s until any index i
and second, the longest subarray consisting of 1’s AND a single 0 until that index i
.
In the code below,
dp[i][0]
= max length of 1’s up and until index i
dp[i][1]
= max length of 1’s with only single 0 until index i
.
Linearly moving through input array nums
we can smartly fill the dp array to obtain our results. The moment we come across two or more 0’s side-to-side, we reset our dp[i][1] = 0
.
Note: This is not a 2D DP problem although the dp[i][j]
might confuse ya. We are using the inner vector/array to keep track of two things at once - it’s essentially the same as using two distinct arrays OR using a vector<pair<int,int>>
.
Code:
C++
Big O Analysis
The time complexity is O(N)
because it iterates through the input nums
list once. (For the unoptimized code, time complexity is O(N * k)
where k = window length.)
The space complexity is O(N)
as well, due to the result vector avgs
we created of length N
.