当前位置:网站首页>LeetCode 209. Minimum length subarray
LeetCode 209. Minimum length subarray
2022-07-02 14:53:00 【HumbleFool】
LeetCode 209. Subarray with the smallest length
The prefix and + Two points
class Solution {
public:
int minSubArrayLen(int target, vector<int>& nums) {
int n = nums.size();
int s[n + 1] = {
0};
for(int i = 1; i <= n; i ++) s[i] = s[i - 1] + nums[i - 1];
int res = 1e9;
for(int i = 1; i <= n; i ++)
{
int t = s[i] - target;
int l = 0, r = n;
while(l < r)
{
int mid = (l + r + 1) >> 1;
if(s[mid] <= t) l = mid;
else r = mid - 1;
}
if(s[r] <= t) res = min(res, i - r);
}
return res == 1e9 ? 0 : res;
}
};
The sliding window / Double pointer
class Solution {
public:
int minSubArrayLen(int target, vector<int>& nums) {
int sum = 0, res = 1e9;
for(int l = 0, r = 0; r < nums.size(); r ++)
{
sum += nums[r];
while(sum >= target)
{
res = min(res, r - l + 1);
sum -= nums[l ++];
}
}
return res == 1e9 ? 0 : res;
}
};
边栏推荐
- Yyds dry goods inventory software encryption lock function
- There is no solution to the decryption error of the remote user 'sa' and the service master password mapped from the remote server 'to the local user' (null) /sa '
- Borui data integrated intelligent observable platform was selected into the "Yunyuan production catalogue" of China Academy of communications in 2022
- fatal: unsafe repository is owned by someone else 的解决方法
- taobao. logistics. dummy. Send (no logistics delivery processing) interface, Taobao store delivery API interface, Taobao order delivery interface, Taobao R2 interface, Taobao oau2.0 interface
- LeetCode 2310. 个位数字为 K 的整数之和
- 广州市应急管理局发布7月高温高湿化工安全提醒
- 华为面试题: 没有回文串
- buuctf-pwn write-ups (7)
- Edit the formula with MathType, and set it to include only mathjax syntax when copying and pasting
猜你喜欢
Reuse and distribution
MQ tutorial | exchange (switch)
taobao.trade.memo.add( 对一笔交易添加备注 )接口,淘宝店铺插旗接口,淘宝订单插旗API接口,oAuth2.0接口
##51单片机实验之简易验证码发生器
Fabric. Usage of JS eraser (including recovery function)
Makefile separates file names and suffixes
Fabric. JS free draw circle
天猫商品详情接口(APP,H5端)
< schematic diagram of oral arithmetic exercise machine program development> oral arithmetic exercise machine / oral arithmetic treasure / children's math treasure / children's calculator LCD LCD driv
Fabric.js 橡皮擦的用法(包含恢复功能)
随机推荐
STM32 standard firmware library function name (I)
< schematic diagram of oral arithmetic exercise machine program development> oral arithmetic exercise machine / oral arithmetic treasure / children's math treasure / children's calculator LCD LCD driv
复用和分用
Threejs controller cube space basic controller + inertia control + flight control
Advanced C language (learn malloc & calloc & realloc & free in simple dynamic memory management)
taobao.trade.memo.add( 对一笔交易添加备注 )接口,淘宝店铺插旗接口,淘宝订单插旗API接口,oAuth2.0接口
taobao.logistics.dummy.send( 无需物流发货处理 )接口,淘宝店铺发货API接口,淘宝订单发货接口,淘宝r2接口,淘宝oAu2.0接口
一张图彻底掌握prototype、__proto__、constructor之前的关系(JS原型、原型链)
天猫商品详情接口(APP,H5端)
PTA题库 ===>复数四则运算,一帮一,考试座位号(7-73)
篇9:XShell免费版安装
MathML to latex
Tmall product details interface (APP, H5 end)
【无标题】LeetCode 2321. 拼接数组的最大分数
taobao.trade.get( 获取单笔交易的部分信息),淘宝店铺订单接口,淘宝oAuth2.0接口,淘宝R2接口代码对接分享
【NOI模拟赛】刮痧(动态规划)
Why can't browsers read JSX?
taobao. trade. memo. Add (add remarks to a transaction) interface, Taobao store flag insertion interface, Taobao order flag insertion API interface, oauth2.0 interface
Available solution development oral arithmetic training machine / math treasure / children's oral arithmetic treasure / intelligent math treasure LCD LCD driver ic-vk1622 (lqfp64 package), original te
STM32标准固件库函数名记忆(二)