当前位置:网站首页>力扣(LeetCode)209. 长度最小的子数组(2022.07.28)
力扣(LeetCode)209. 长度最小的子数组(2022.07.28)
2022-07-29 06:53:00 【ChaoYue_miku】
给定一个含有 n 个正整数的数组和一个正整数 target 。
找出该数组中满足其和 ≥ target 的长度最小的 连续子数组 [numsl, numsl+1, …, numsr-1, numsr] ,并返回其长度。如果不存在符合条件的子数组,返回 0 。
示例 1:
输入:target = 7, nums = [2,3,1,2,4,3]
输出:2
解释:子数组 [4,3] 是该条件下的长度最小的子数组。
示例 2:
输入:target = 4, nums = [1,4,4]
输出:1
示例 3:
输入:target = 11, nums = [1,1,1,1,1,1,1,1]
输出:0
提示:
1 <= target <= 109
1 <= nums.length <= 105
1 <= nums[i] <= 105
进阶:
如果你已经实现 O(n) 时间复杂度的解法, 请尝试设计一个 O(n log(n)) 时间复杂度的解法。
来源:力扣(LeetCode)
链接:https://leetcode.cn/problems/minimum-size-subarray-sum
方法一:滑动窗口
C++提交内容:
class Solution {
public:
int minSubArrayLen(int s, vector<int>& nums) {
int n = nums.size();
if (n == 0) {
return 0;
}
int ans = INT_MAX;
int start = 0, end = 0;
int sum = 0;
while (end < n) {
sum += nums[end];
while (sum >= s) {
ans = min(ans, end - start + 1);
sum -= nums[start];
start++;
}
end++;
}
return ans == INT_MAX ? 0 : ans;
}
};
边栏推荐
- 微服务远程调用
- Flink real-time warehouse DWD layer (processing complex data - installation and replacement of streams and tables) template code
- gcc/g++的使用
- Latest 10 billion quantitative private placement list
- win11VMware打开虚拟机就蓝屏重启以及启动不了的解决方案
- Interface test actual project 03: execute test cases
- Ethernet interface introduction
- Vmware16 create virtual machine: cannot create a new virtual machine, do not have permission to perform this operation
- 2022-07-28:以下go语言代码输出什么?A:AA;B:AB;C:BA;D:BB。 package main import ( “fmt“ ) func main() { f
- Spark Learning Notes (VII) -- spark core core programming - RDD serialization / dependency / persistence / partition / accumulator / broadcast variables
猜你喜欢

Vmware16 create virtual machine: win11 cannot be installed

数组的子集能否累加出K

Cvpr2021 | multi view stereo matching based on self supervised learning (cvpr2021)

Redis Basics

Decompilation of wechat applet

MySQL 高级(进阶) SQL 语句 (一)

Error 1045 (28000) access denied for user 'root' @ 'localhost' solution

女研究生做“思维导图”与男友吵架!网友:吵架届的“内卷之王”....
![[OpenGL] use of shaders](/img/73/1322afec8add6462ca4b82cb8112d1.png)
[OpenGL] use of shaders

CMOS芯片制造全工艺流程
随机推荐
Other basic monitoring items of ZABBIX
Flink real-time warehouse DWD layer (processing complex data - installation and replacement of streams and tables) template code
Thoroughly understand kubernetes scheduling framework and plug-ins
CAN&CANFD综合测试分析软件LKMaster与PCAN-Explorer 6分析软件的优势对比
MVFuseNet:Improving End-to-End Object Detection and Motion Forecasting through Multi-View Fusion of
Flink real time warehouse DWD layer (traffic domain) template code
Redis基础篇
Summary of OCR optical character recognition methods
Kubernetes (V) -- deploy kubernetes dashboard
It's enough for MySQL to have this article (disgusting and crazy typing 37k words, just for Bo Jun's praise!!!)
Use vscode to configure Mysql to realize connection, query, and other functions
数组的子集不能累加出的最小正数
Kubernetes (五) ---------部署 Kubernetes Dashboard
Section 7 - compilation of programs (preprocessing operations) + links
ERROR 1045 (28000) Access denied for user ‘root‘@‘localhost‘解决方法
After three years of outsourcing, the salary of automatic testing after job hopping is twice that of the original. The secret is
VMware16安装虚拟机遇到的问题
[Charles' daily problems] when you open Charles, you can't use nails
Problems encountered in vmware16 installing virtual machines
Zabbix 其他基础监控项