Py3 C++ sliding window速解Leetcode1493  Longest Subarray of 1's After Deleting One Element
示範如何從一般  sliding window解答簡化成單迴圈解答解無if
---
Demonstrates how to simplify a general sliding window solution into a single loop solution without if
[codes on Leetcode]https://leetcode.com/problems/longest-subarray-of-1s-after-deleting-one-element/solutions/7114727/sliding-window-single-loop-without-if-beats-100/
[sliding window/2-pointer playlist]https://www.youtube.com/watch?v=pi1XpQU_Yxo&list=PLYRlUBnWnd5KrCfs7qCFb-Bvn7dvnakFy
class Solution:
    def longestSubarray(self, nums: List[int]) -> int:
        n, l, zeros, ans=len(nums), 0, 0, 0
        for r, x in enumerate(nums):
            zeros+=(x==0)
            if zeros>1:
                zeros-=(nums[l]==0)
                l+=1
            ans=max(ans, r-l)
        return ans
            
        
 
   
 
 
沒有留言:
張貼留言