当前位置:网站首页>Array -- 209. Subarray with the smallest length
Array -- 209. Subarray with the smallest length
2022-07-23 22:15:00 【Xiao Zhao, who is working hard for millions of annual salary】
1 Title Description
- Subarray with the smallest length
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 .
2 Title Example
Example 1:
Input :target = 7, nums = [2,3,1,2,4,3]
Output :2
explain : Subarray [4,3] Is the smallest subarray in this condition .
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
3 Topic tips
- 1 <= target <= 109
- 1 <= nums.length <= 105
- 1 <= nums[i] <= 105
4 Ideas
The sliding window
So called sliding window , It's constantly adjusting the starting and ending positions of subsequences , So we can get the result that we want to think about .
First of all, think about If you use one for loop , Then it should mean The starting position of the sliding window , Or end position .
If only one for Loop to express The starting position of the sliding window , So how to traverse the remaining termination positions ?
The key to solving the problem is How the start of the window moves
The beauty of sliding windows is based on the current subsequence and size , Constantly adjust the starting position of subsequences . So that O(n^2) The violent solution is reduced to O(n)
therefore , The index of this loop , It must mean The end position of the sliding window .
5 My answer
class Solution {
// The sliding window
public int minSubArrayLen(int s, int[] nums) {
int left = 0;
int sum = 0;
int result = Integer.MAX_VALUE;
for (int right = 0; right < nums.length; right++) {
sum += nums[right];
while (sum >= s) {
result = Math.min(result, right - left + 1);
sum -= nums[left++];
}
}
return result == Integer.MAX_VALUE ? 0 : result;
}
}
边栏推荐
- Sixty final review questions of software architecture
- 02.网页结构相关知识补充
- Redis common commands correspond to redisson object operations
- lambda学习(sort后面的Comparator的使用,collection后使用Collectors.groupingBy分组)
- 人生总需要一点激情
- [mathematical modeling summer training] location of distribution center
- Pulsar open source message queue_ Understand pulsar --- pulsar work notes 001
- 【golang学习笔记】flag包的简单使用,命令行解析
- MySQL的JDBC编程
- 小说里的编程 【连载之十八】元宇宙里月亮弯弯
猜你喜欢

10道面试基础笔试题,你能对几题?

Leetcode high frequency question 53. maximum subarray sum, continuous subarray with maximum sum, return its maximum sum

Application of performance test knowledge to actual combat

【golang学习笔记】包(package)的使用

U++ 学习笔记 控制物体Scale

人生总需要一点激情

Inspiration from Buffett's shareholders' meeting 2021-05-06

Profit logic of DFI project 2021-04-26

王学岗视频编码————MediaCodec编解码

记忆化搜索 - DP
随机推荐
多线程问题:为什么不应该使用多线程读写同一个socket连接?
pulsar开源消息队列_了解Pulsar---Pulsar工作笔记001
02. Supplement of knowledge related to web page structure
大学数据库创建与查询实战——数据库表设计
Programmation JDBC pour MySQL
[golang learning notes] simple use of flag package, command line parsing
众邦科技又一潜心力作 —— 陀螺匠 OA 系统
Leetcode high frequency question 53. maximum subarray sum, continuous subarray with maximum sum, return its maximum sum
淘宝助理停用,用大淘营导入数据包上传宝贝提示“主图为必填项,不能为空”是什么原因?如何解决?
JS - event proxy and application scenarios
Life always needs a little passion
JS object array de duplication
JDBC programming of MySQL
Comment forcer complètement le meurtre de processus indépendants de l'arrière - plan?
Leetcode high frequency question 62. different paths: how many paths does the robot have from the upper left corner to the lower right corner? Pure probability permutation and combination problem, not
LeetCode高频题62. 不同路径:机器人从左上角到右下角的路径有多少条?纯概率排列组合问题,而不是动态规划题
el-select下拉框多选远程搜索反显
U++ 学习笔记 控制物体Scale
U++ 事件
How does MySQL prepare SQL (solve the problem that in query SQL preprocessing can only query one record)