当前位置:网站首页>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;
}边栏推荐
- Power Spectral Density Estimates Using FFT---MATLAB
- YYGH-BUG-04
- Cmake cross compilation
- PyTorch nn. Full analysis of RNN parameters
- 时间格式化显示
- XSS labs master shooting range environment construction and 1-6 problem solving ideas
- Repeat, tile and repeat in pytorch_ The difference between interleave
- HOW TO CREATE A BEAUTIFUL INTERACTIVE HEATMAP IN R
- Esp32 audio frame esp-adf add key peripheral process code tracking
- Thesis translation: 2022_ PACDNN: A phase-aware composite deep neural network for speech enhancement
猜你喜欢

Research on and off the Oracle chain

HR wonderful dividing line

FLESH-DECT(MedIA 2021)——一个material decomposition的观点

HOW TO CREATE A BEAUTIFUL INTERACTIVE HEATMAP IN R

How does Premiere (PR) import the preset mogrt template?

Applet link generation

Data analysis - Matplotlib sample code

conda常用命令汇总

数据分析 - matplotlib示例代码

Lekao: contents of the provisions on the responsibility of units for fire safety in the fire protection law
随机推荐
Industry analysis
b格高且好看的代码片段分享图片生成
to_bytes与from_bytes简单示例
Leetcode122 买卖股票的最佳时机 II
Leetcode topic [array] -540- single element in an ordered array
YYGH-BUG-05
Pytorch builds LSTM to realize clothing classification (fashionmnist)
Fabric. JS 3 APIs to set canvas width and height
H5, add a mask layer to the page, which is similar to clicking the upper right corner to open it in the browser
机械臂速成小指南(七):机械臂位姿的描述方法
Leetcode739 每日温度
Easyexcel and Lombok annotations and commonly used swagger annotations
PHP query distance according to longitude and latitude
HOW TO EASILY CREATE BARPLOTS WITH ERROR BARS IN R
字符串回文hash 模板题 O(1)判字符串是否回文
HOW TO CREATE A BEAUTIFUL INTERACTIVE HEATMAP IN R
[geek challenge 2019] upload
自然语言处理系列(一)——RNN基础
Leetcode922 按奇偶排序数组 II
K-Means Clustering Visualization in R: Step By Step Guide