Re: [闲聊] 每日LeetCode

楼主: Rushia (みけねこ的鼻屎)   2023-07-10 01:10:04
https://leetcode.com/problems/longest-subarray-of-1s-after-deleting-one-element/description
1493. Longest Subarray of 1's After Deleting One Element
给你一个只包含 0 和 1 的阵列,求出一个连续子序列,他需满足:
1.子序列中最多只能有一个0(可以删除这个0)。
2.为阵列中的最长子序列。
Example 1:
Input: nums = [1,1,0,1]
Output: 3
Explanation: After deleting the number in position 2, [1,1,1] contains 3
numbers with value of 1's.
Example 2:
Input: nums = [0,1,1,1,0,1,1,0,1]
Output: 5
Explanation: After deleting the number in position 4, [0,1,1,1,1,1,0,1]
longest subarray with value of 1's is [1,1,1,1,1].
Example 3:
Input: nums = [1,1,1]
Output: 2
Explanation: You must delete one element.
思路:
1.找连续子序列一样是先想到滑动窗口,窗口每次都push一个元素,当窗口内的0数量
大于 1 的时候,把左边的元素 pop 直到 0 的数量等于 1。
2.过程中不断更新 res 最大值。
Java Code:

Links booklink

Contact Us: admin [ a t ] ucptt.com