当前位置:网站首页>Leetcode209 subarray with the smallest length
Leetcode209 subarray with the smallest length
2022-07-02 12:05:00 【Monsters 114】
Given a containing n An array of positive integers and a positive integer target .
Find the sum of the array ≥ target The smallest length of Continuous subarray [numsl, numsl+1, ..., numsr-1, numsr] , And return its length . If there is no sub array that meets the conditions , return 0 .
The sliding window :
Define two pointers start and end Each represents a subarray ( Sliding window ) Start and end positions of , Maintenance variables sum Stores the elements and in the subarray nums[start] To nums[end] Elements and .
In the initial state ,start and end All point to subscripts 0,sum The value of is 0.
Every iteration , take nums[end] Add to sum, If sum≥s, Then update the minimum length of the subarray ( At this point, the length of the subarray is end−start+1), And then nums[start] from sum Subtract from and start Move right , until sum<s, In this process, the minimum length of the subarray is also updated . At the end of each iteration , take end Move right .
public int minSubArrayLen(int target, int[] nums) {
int start = 0;
int len = Integer.MAX_VALUE;
int sum = 0;
for(int end = 0; end < nums.length; end++){
sum += nums[right];
while(sum >= target){
len = Math.min(len,end-start+1);
sum -= nums[start++];
}
}
return len == Integer.MAX_VALUE ? 0 : len;
}
边栏推荐
- HOW TO EASILY CREATE BARPLOTS WITH ERROR BARS IN R
- Yygh-10-wechat payment
- 基于Arduino和ESP8266的连接手机热点实验(成功)
- 字符串回文hash 模板题 O(1)判字符串是否回文
- 6. Introduce you to LED soft film screen. LED soft film screen size | price | installation | application
- HOW TO CREATE AN INTERACTIVE CORRELATION MATRIX HEATMAP IN R
- Leetcode122 买卖股票的最佳时机 II
- Leetcode922 按奇偶排序数组 II
- SVO2系列之深度濾波DepthFilter
- K-Means Clustering Visualization in R: Step By Step Guide
猜你喜欢
YYGH-BUG-05
Natural language processing series (I) -- RNN Foundation
Data analysis - Matplotlib sample code
Flesh-dect (media 2021) -- a viewpoint of material decomposition
The position of the first underline selected by the vant tabs component is abnormal
深入理解PyTorch中的nn.Embedding
SVO2系列之深度滤波DepthFilter
Depth filter of SvO2 series
GGHIGHLIGHT: EASY WAY TO HIGHLIGHT A GGPLOT IN R
HOW TO EASILY CREATE BARPLOTS WITH ERROR BARS IN R
随机推荐
[visual studio 2019] create and import cmake project
Take you ten days to easily finish the finale of go micro services (distributed transactions)
This article takes you to understand the operation of vim
自然语言处理系列(一)——RNN基础
HOW TO EASILY CREATE BARPLOTS WITH ERROR BARS IN R
XSS labs master shooting range environment construction and 1-6 problem solving ideas
FastDateFormat为什么线程安全
ORB-SLAM2不同线程间的数据共享与传递
Beautiful and intelligent, Haval H6 supreme+ makes Yuanxiao travel safer
PX4 Position_ Control RC_ Remoter import
HOW TO CREATE AN INTERACTIVE CORRELATION MATRIX HEATMAP IN R
The selected cells in Excel form have the selection effect of cross shading
Deep understanding of NN in pytorch Embedding
Leetcode739 每日温度
Flesh-dect (media 2021) -- a viewpoint of material decomposition
YYGH-BUG-04
小程序链接生成
SCM power supply
测试左移和右移
Analyse de l'industrie