当前位置:网站首页>Force buckle 209 Minimum length subarray
Force buckle 209 Minimum length subarray
2022-06-30 04:56:00 【A wise man should not be bald】
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 .

source : Power button (LeetCode)
link :https://leetcode.cn/problems/minimum-size-subarray-sum
Copyright belongs to the network . For commercial reprint, please contact the official authority , Non-commercial reprint please indicate the source .
Method : The sliding window ( Variable window size )
class Solution {
public:
/* Violence solution : Not recommended , The time complexity is O(n^2), The force buckle will not pass , Time limit exceeded
Time complexity :O(n^2) Spatial complexity :O(1)*/
/*
int minSubArrayLen(int target, vector<int>& nums) {
int result = INT32_MAX; // Final result , The shortest continuous subarray
int subLength = 0; // The sum of the values of the subsequence currently traversed
int size = nums.size();
for(int i = 0; i < size; ++i) { // Set subsequence int sum = 0;
for(int j = i; j < size; ++j) { // Set the end point of subsequence to j
sum += nums[j];
if(sum >= target) { // Update once the conditions are met result Size
subLength = j - i + 1;
result = result < subLength ? result : subLength;
break; // The current shortest subsequence has been found , So direct break
}
}
}
// If result Not assigned , It indicates that there is no continuous subsequence that satisfies the condition , return 0
return result == INT32_MAX ? 0 : result;
}
*/
/* Recommended solution : The sliding window */
int minSubArrayLen(int target, vector<int>& nums) {
int result = INT32_MAX; // The result returned , The initial value is set to the maximum value , It will be updated continuously later
int subLength = 0; // The length of the continuous subsequence that meets the return condition currently traversed , The length of the sliding window
int size = nums.size(); // The size of the given array
int i = 0; // The starting position of the sliding window
int sum = 0; // The sum of the sequence currently traversed , The sum of the sliding window values
for(int j = 0; j < size; ++j) {
sum += nums[j];
// Be sure to use while Out of commission if, Each update i( The starting position ), It is necessary to constantly compare whether the subsequence meets the conditions
while(sum >= target) {
subLength = j - i + 1; // Take the length of the subsequence
result = result < subLength ? result : subLength;
sum -= nums[i++]; // The essence of sliding windows , Constantly changing i The starting position
}
}
// If result Not assigned , It indicates that there is no continuous subsequence that satisfies the condition , return 0
return result == INT32_MAX ? 0 : result;
// Time complexity :O(n) Spatial complexity :O(1)
}
};
边栏推荐
- Introduction to some representations, neighbors and degrees of Graphs
- How to apply for SSL certificate from the manufacturer
- Foreign SSL certificate
- 0 basic unity course. Bricklaying
- Moore Manor diary I: realize the reclamation, sowing, watering and harvest in Moore Manor
- Unity lens making
- Important knowledge points in unity3d
- A virtual reality secret room escape adventure, let you see Technology Singapore
- Li Kou 2049: count the number of nodes with the highest score
- Modbus protocol register
猜你喜欢

Unity Logitech steering wheel access

Connect to the database and run node JS running database shows that the database is missing

Unreal 4 unavigationsystemv1 compilation error

Moore Manor diary I: realize the reclamation, sowing, watering and harvest in Moore Manor

Wildcard SSL certificate issuing time

Have a heart beating Valentine's day in Singapore

What is multimodal interaction?

Beanfactory creation process

Autowired注解警告的解决办法

Unity is associated with vs. there is a compiler problem when opening
随机推荐
图的一些表示方式、邻居和度的介绍
LxC and LXD container summary
0 basic unity course. Bricklaying
力扣59. 螺旋矩阵 II
Draw on screen border in Commodore 64
The role of break
The most comprehensive summary notes of redis foundation + advanced project in history
Force buckle 349 Intersection of two arrays
Some books you should not miss when you are new to the workplace
【Paper】2017_ Research on coordinated control method of underwater vehicle formation marine survey
Create transfer generation point
A collection of errors encountered in machine learning with unity
Qos(Quality of Service)
One interview question and one joint index every day
Unity ontriggerenter does not call
Unity3d realizes Google Digital Earth
Sailing experience not to be missed in New York Tourism: take you to enjoy the magnificent city scenery from different perspectives
Webots notes day 2
【Paper】2015_ Coordinated cruise control for high-speed train movements based on a multi-agent model
Introduction to some representations, neighbors and degrees of Graphs