Re: [闲聊] 每日LeetCode

楼主: Rushia (みけねこ的鼻屎)   2023-07-09 01:32:58
https://leetcode.com/problems/minimum-size-subarray-sum/description/
209. Minimum Size Subarray Sum
给你一个阵列 nums 和一个数字 target,找出最小的连续子序列长,该子序列的和
需要大于等于 target(不能的话返回0)。
Example 1:
Input: target = 7, nums = [2,3,1,2,4,3]
Output: 2
Explanation: The subarray [4,3] has the minimal length under the problem
constraint.
Example 2:
Input: target = 4, nums = [1,4,4]
Output: 1
Example 3:
Input: target = 11, nums = [1,1,1,1,1,1,1,1]
Output: 0
思路:
1.因为子序列是连续的所以直觉的想到滑动窗口,每次都先push一个数字,直到和大于
target为止。
2.当和大于等于target时,检查pop掉最左边的数字时是否也可以满足 sum >= target,
如果可以就一直 pop 掉窗口最左元素直到不满足。
3.承2,pop到不能pop之后,再用目前的最小窗口长度更新答案。
Java Code:

Links booklink

Contact Us: admin [ a t ] ucptt.com