current position:Home>"Double pointer" leetcode 209. Minimum length subarray (medium)
"Double pointer" leetcode 209. Minimum length subarray (medium)
2021-08-27 08:53:31 【_ Monday】
One 、 Understand the topic
Attach a link to the original title :209. 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
Continuous subarray with the smallest length of [numsl, numsl+1, ..., numsr-1, numsr]
, And return its length . If there is no sub array that meets the conditions , return 0
.
Example :
Input :target = 7, nums = [2,3,1,2,4,3]
Output :2
explain : Subarray [4,3] Is the smallest subarray in this condition .
Copy code
Two 、 Problem analysis
According to the meaning of the above question , Let's take a look at the specific Their thinking :
- Define two pointers
l
andr
, Point to the array with the subscript0
The location of . Move only one pointer at a time , Step by step to1
; l
The pointer doesn't understand ,r
Pointer backward . untilsum >= target
, Record the minimum window length at this timemin
;l
Pointer backward ,r
The pointer doesn't move . Judgesum
Greater than or equal totarget
, If yes, reduce the minimum window lengthmin
, Otherwise repeat the steps 2;min
The value cannot exceed the maximum integer limit .
3、 ... and 、 Code implementation
According to the above question , We will use js
To achieve this problem . The specific implementation code is as follows :
/** * @param {number} target * @param {number[]} nums * @return {number} */
let minSubArrayLen = function(target, nums){
const len = nums.length;
// 1. Define the left pointer , Point to the head of the array
let l = 0;
// 2. Minimum window length for storage , Initialization length is array length + 1
let min = len + 1;
// 3. The sum of the array
let sum = 0;
// 4. Define the right pointer , Let the right pointer traverse the entire array
for(let r = 0; r < len; r++){
// 4.1 Keep moving the right pointer to the right , And add it to the previous number
sum += nums[r];
// 4.2 When you meet sum Greater than or equal to the target value
while(sum >= target){
// 4.2.1 Reduce a window value
sum -= nums[l];
// 4.2.2 Determine the size of the sliding window and the smallest sliding window
min = Math.min(r - l + 1, min);
// 4.2.3 Move the left pointer one bit to the right
l++;
}
}
// 5. Determine whether the minimum value exceeds the actual array length , If it exceeds, there is no qualified array , return 0; Otherwise, the minimum value is returned min value
return min === len + 1 ? 0 : min;
}
console.log(minSubArrayLen(7,[2,3,1,2,4,3])); // 2
Copy code
That's about Subarray with the smallest length The antithesis of , I don't know if it will help my friends ?
See you next time
copyright notice
author[_ Monday],Please bring the original link to reprint, thank you.
https://en.qdmana.com/2021/08/20210827085327275b.html
The sidebar is recommended
- Crazy blessing! Tencent boss's "million JVM learning notes", real topic of Huawei Java interview 2020-2021
- JS JavaScript how to get the subscript of a value in the array
- How to implement injection in vuex source code?
- JQuery operation select (value, setting, selected)
- One line of code teaches you how to advertise on Tanabata Valentine's Day - Animation 3D photo album (music + text) HTML + CSS + JavaScript
- An article disassembles the pyramid architecture behind the gamefi outbreak
- BEM - a front-end CSS naming methodology
- [vue3] encapsulate custom global plug-ins
- Error using swiper plug-in in Vue
- Another ruthless character fell by 40000, which was "more beautiful" than Passat and maiteng, and didn't lose BMW
guess what you like
-
Huang Lei basks in Zhang Yixing's album, and the relationship between teachers and apprentices is no less than that in the past. Netizens envy Huang Lei
-
He was cheated by Wang Xiaofei and Li Chengxuan successively. Is an Yixuan a blessed daughter and not a blessed home?
-
Zhou Shen sang the theme song of the film "summer friends and sunny days" in mainland China. Netizen: endless aftertaste
-
Pink is Wangyuan online! Back to the peak! The new hairstyle is creamy and sassy
-
Front end interview daily 3 + 1 - day 858
-
Spring Webflux tutorial: how to build reactive web applications
-
[golang] walk into go language lesson 24 TCP high-level operation
-
August 23, 2021 Daily: less than three years after its establishment, Google dissolved the health department
-
The female doctor of Southeast University is no less beautiful than the female star. She has been married four times, and her personal experience has been controversial
-
There are many potential safety hazards in Chinese restaurant. The top of the program recording shed collapses, and the artist will fall down if he is careless
Random recommended
- Anti Mafia storm: He Yun's helpless son, Sun Xing, is destined to be caught by his dry son
- Introduction to flex flexible layout in CSS -- learning notes
- CSS learning notes - Flex layout (Ruan Yifeng tutorial summary)
- Today, let's talk about the arrow function of ES6
- Some thoughts on small program development
- Talk about mobile terminal adaptation
- Unwilling to cooperate with Wang Yibo again, Zhao Liying's fans went on a collective strike and made a public apology in less than a day
- JS function scope, closure, let, const
- Zheng Shuang's 30th birthday is deserted. Chen Jia has been sending blessings for ten years. Is it really just forgetting to make friends?
- Unveil the mystery of ascension
- Asynchronous solution async await
- Analysis and expansion of Vue infinite scroll source code
- Compression webpack plugin first screen loading optimization
- Specific usage of vue3 video play plug-in
- "The story of huiyeji" -- people are always greedy, and fairies should be spotless!
- Installing Vue devtool for chrome and Firefox
- Basic usage of JS object
- 1. JavaScript variable promotion mechanism
- Two easy-to-use animation JS that make the page move
- Front end Engineering - scaffold
- Java SQL Server intelligent fixed asset management, back end + front end + mobile end
- Mediator pattern of JavaScript Design Pattern
- Array de duplication problem solution - Nan recognition problem
- New choice for app development: building mobile applications using Vue native
- New gs8 Chengdu auto show announces interior Toyota technology blessing
- Vieira officially terminated his contract and left the team. The national security club sent blessings to him
- Less than 200000 to buy a Ford RV? 2.0T gasoline / diesel power, horizontal bed / longitudinal bed layout can be selected
- How does "heart 4" come to an end? Pinhole was boycotted by the brand, Ma Dong deleted the bad comments, and no one blessed him
- We are fearless in epidemic prevention and control -- pay tribute to the front-line workers of epidemic prevention!
- Front end, netty framework tutorial
- Xiaomi 11 | miui12.5 | android11 solves the problem that the httpcanary certificate cannot be installed
- The wireless charging of SAIC Roewe rx5 plus is so easy to use!
- Upload and preview pictures with JavaScript, and summarize the most complete mybatis core configuration file
- [25] typescript
- CSS transform Complete Guide (Second Edition) flight.archives 007
- Ajax foundation - HTTP foundation of interview essential knowledge
- Cloud lesson | explain in detail how Huawei cloud exclusive load balancing charges
- Decorator pattern of JavaScript Design Pattern
- [JS] 10. Closure application (loop processing)
- Left hand IRR, right hand NPV, master the password of getting rich