当前位置:网站首页>209. Subarray with the smallest length (array)
209. Subarray with the smallest length (array)
2022-04-23 10:15:00 【Popuessing's Jersey】
subject :
Given a containing n An array of positive integers and a positive integer s , Find the sum of the array ≥ s The smallest length of continuity Subarray , And return its length . If there is no sub array that meets the conditions , return 0.
Example :
Input :s = 7, nums = [2,3,1,2,4,3] Output :2 explain : Subarray [4,3] Is the smallest subarray in this condition .
Method 1 : Violence solution
public int minSubArrayLen(int s,int[] nums){
int n = nums.length;
// Sum of subsequences
int sum =0;
// Subsequence length
int subLength =0;
int res = Integer.MAX_VALUE;
for (int i = 0; i <n ; i++) {
// Set the starting point of the subsequence
sum=0;
for (int j = i; j <n ; j++) {
// Set the end point of the subsequence
sum+=nums[j];
if(sum >=s){
// Update subsequence length
subLength = j-i+1;
res = Math.min(res, subLength);
break;// If you find the minimal array , Jump out of the second loop that sets the end point of the subsequence , Add the starting point of subsequence and continue to traverse
}
}
}
// If res If it's not assigned , Just go back to 0, There are no subsequences that meet the conditions
return res == Integer.MAX_VALUE ? 0:res;
}
Time complexity :O(n^2)
Spatial complexity :O(1)
Method 2 : The sliding window ( Double pointer )
Ideas : Use the left and right pointers to dynamically maintain and update the data s The smallest subarray of
// Method 2 : The sliding window ( Double pointer )
public int minSubArrayLen(int s,int[] nums){
// The length of the array
int n = nums.length;
// Sum of subsequences
int sum = 0;
// Subsequence length
int subLength = 0;
// Subsequence results
int result = Integer.MAX_VALUE;
// Slide window start position
int left =0;
for (int right=0;right<n;right++) {
sum+= nums[right];
while (sum>=s){
subLength = right-left+1;
result = Math.min(result, subLength);
sum-=nums[left++];
}
}
return result==Integer.MAX_VALUE? 0: result;
}
Time complexity :O(n)
Spatial complexity :O(1)
Why? for Nested inside the loop while The cycle time complexity is O(n)?
Because time complexity measures the number of repetitions of each element , In the sliding window , The element appears once when entering the window , Appears once when leaving the window . So each element is manipulated twice , The time complexity should be O(2n), That is to say O(n) 了 .
版权声明
本文为[Popuessing's Jersey]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204231011141309.html
边栏推荐
- DBA常用SQL语句(6)- 日常管理
- Chapter 3 enable and adjust the size of IM column storage (im-3.1)
- 杰理之有时候定位到对应地址的函数不准确怎么办?【篇】
- 计算机网络安全实验二|DNS协议漏洞利用实验
- 杰理之通常影响CPU性能测试结果的因素有:【篇】
- "Gu Yu series" airdrop
- 997、有序数组的平方(数组)
- Odoo 服务器搭建备忘
- Sim Api User Guide(4)
- Computer network security experiment II DNS protocol vulnerability utilization experiment
猜你喜欢
随机推荐
Turn: Maugham: reading is a portable refuge
What are the system events of Jerry's [chapter]
JVM——》常用命令
ansible playbook语法和格式 自动化云计算
Chapter 1 Oracle database in memory related concepts (im-1.1)
Function realization of printing page
MapReduce核心和基础Demo
定义链表(链表)
Using multithreading to output abc10 times in sequence
Sim Api User Guide(5)
Question bank and answers of Shanghai safety officer C certificate examination in 2022
域名和IP地址的联系
59、螺旋矩阵(数组)
MapReduce压缩
DBA常用SQL语句(1)— 概况信息
Common DBA SQL statements (4) - Top SQL
Realize data value through streaming data integration (1)
101. Symmetric Tree
2022年上海市安全员C证考试题库及答案
实践六 Windows操作系统安全攻防