All prompts are owned by LeetCode. To view the prompt, click the title link above.
First completed : February 17, 2025
Last updated : February 17, 2025
Related Topics : Array, Dynamic Programming, Sliding Window
Acceptance Rate : 68.85 %
class Solution:
def longestSubarray(self, nums: List[int]) -> int:
ones = [0]
while nums :
curr = nums.pop()
if curr and ones and ones[-1] :
ones[-1] += 1
continue
if curr :
ones.append(1)
continue
ones.append(0)
ones.append(0)
if len(ones) == 3 :
return max(0, sum(ones) - 1)
maxx = 0
for i in range(1, len(ones) - 1) :
maxx = max(maxx, ones[i - 1] + ones[i] + ones[i + 1])
return maxx