Skip to content

Latest commit

 

History

History
55 lines (37 loc) · 1.27 KB

_1493. Longest Subarray of 1's After Deleting One Element.md

File metadata and controls

55 lines (37 loc) · 1.27 KB

All prompts are owned by LeetCode. To view the prompt, click the title link above.

Back to top


First completed : February 17, 2025

Last updated : February 17, 2025


Related Topics : Array, Dynamic Programming, Sliding Window

Acceptance Rate : 68.85 %


Solutions

Python

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