当前位置:网站首页>【LeetCode】209. Minimum length subarray
【LeetCode】209. Minimum length subarray
2022-06-12 22:22:00 【LawsonAbs】
0 summary
- A problem can be implemented in many ways , The best is not necessarily the best . Some may be difficult to implement , Is worth learning .
1 subject
2 thought
2.1 The prefix and + Traverse
Use the prefix and to solve the problem , Double loop to find the minimal array that satisfies the meaning of the question , Complexity is O(nm), among m Is to satisfy the continuous and >=target Minimum sequence length of . This method ( My thoughts ) Compared with the latter two methods , It's really bad .
2.2 The prefix and + The sliding window
In fact, the best way to solve this problem is to use the sliding window method , Time complexity is the lowest .
2.3 The prefix and + Dichotomy
To be updated
3 Code
3.1
The following codes correspond to The prefix and + Traverse Thought
class Solution:
def minSubArrayLen(self, target: int, nums: List[int]) -> int:
pre_sum = [0] * (len(nums)+1)
nums = [0] + nums # There is one in front 0
pre_sum[0] = nums[0]
for i in range(1,len(nums)):
pre_sum[i] = pre_sum[i-1] + nums[i]
if target <= nums[i]:
return 1
if pre_sum[-1] < target:
return 0
elif pre_sum[-1] == target:
return len(nums)-1
else:
length = len(nums)
for i in reversed(range(len(nums))):
for j in reversed(range(0,i-1)): # Find an interval
if (j+length <= i): # If the interval has exceeded length, Is not the optimal solution
break
# print(i,j,pre_sum[i] - pre_sum[j])
if pre_sum[i] - pre_sum[j] >=target:
length = min(length,i-j)
break
return length
边栏推荐
- 【LeetCode】300.最长上升子序列
- Modstartcms modular station building system v3.3.0 component function upgrade, event triggering enhancement
- Ansible playbook和Ansible Roles(三)
- [sword finger offer simple] sword finger offer 24 Reverse linked list
- Configuring Dingding notification of SQL audit platform archery
- China's alternative sports equipment market trend report, technology dynamic innovation and market forecast
- Use group_ Dplyr issues when using group_ by(multiple variables)
- be careful! Your Navicat may have been poisoned
- Ansible roles project case (IV)
- 【LeetCode】数组中第K大的元素
猜你喜欢

How to prevent phishing emails? S/mime certificate to help!

MySQL introduction and installation (I)

JVM foundation - > three ⾊ mark

The interface testing tool apipos3.0 is applicable to process testing and reference parameter variables

flutter系列之:flutter中常用的GridView layout详解

About the solution to "the application cannot start normally 0xc00000022" after qt5.15.2 is installed and qtcreator is started

Ansible summary (VI)

【概率论与数理统计】期末复习抱佛脚:公式总结与简单例题(完结)

反走样/抗锯齿技术

LNMP platform docking redis service
随机推荐
项目里面的traceID的设计
【LeetCode】209. 长度最小的子数组
Ansible summary (VI)
Su embedded training day13 - file IO
The interface testing tool apipos3.0 is applicable to process testing and reference parameter variables
What is embedded
Open source background management system suitable for outsourcing projects
Qt Quick 3D学习:使用鼠标键盘控制节点位置和方向
[medium] 78 Subset (backtracking shall be supplemented later)
China Aquatic Fitness equipment market trend report, technical innovation and market forecast
Use group_ Dplyr issues when using group_ by(multiple variables)
打新债开户安全么,新手该怎么操作?
JVM foundation > G1 garbage collector
【Proteus仿真】简易数码管定时器时钟
Generate the chrysanthemum code of the applet (generate the chrysanthemum code, change the middle logo, change the image size, and add text)
Jin AI her power | impact tech, she can
JVM foundation - what is the process of loading > objects into the JVM, and then clearing them by GC?
Lambda expression and flow optimization code
Ansible PlayBook et ansible roles (3)
42岁大厂高管,给30岁-39岁人提个醒:这6个让你变强的习惯,要尽快养成