当前位置:网站首页>【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
边栏推荐
- Economist focuses on WTO MC12: digital economy may become an important issue
- 数据库每日一题---第10天:组合两个表
- Kotlin collaboration process - flow
- [sword finger offer] sword finger offer 35 Replication of complex linked list
- [probability theory and mathematical statistics] final review: formula summary and simple examples (end)
- 反走样/抗锯齿技术
- Design a MySQL table for message queue to store message data
- JVM foundation - > three ⾊ mark
- 認識的幾比特清華同學都離職了……
- Yyds dry inventory insider news: Series high-frequency interview questions, worth a visit!
猜你喜欢
Leetcode: the maximum number of building change requests that can be reached (if you see the amount of data, you should be mindless)
孙老师版本JDBC(2022年6月12日21:34:25)
设计消息队列存储消息数据的 MySQL 表格
flutter系列之:flutter中常用的GridView layout详解
Ansible playbook and ansible roles (III)
Yyds dry inventory insider news: Series high-frequency interview questions, worth a visit!
【数据分析】基于 kmeans实现数据聚类分组含Matlab源码
[data analysis] data clustering and grouping based on kmeans, including Matlab source code
Things about the kotlin collaboration process - pipeline channel
2021 rust survey results released: 9354 questionnaires collected
随机推荐
设计消息队列存储消息数据的 MySQL 表格
管线中的坐标变换
PE installation win10 system
Qt Quick 3D学习:使用鼠标键盘控制节点位置和方向
The programmer dedicated to promoting VIM has left. Father of vim: I will dedicate version 9.0 to him
Prefix sum and difference
Kotlin collaboration process - flow
How to abstract a problem into a 0-1 knapsack problem in dynamic programming
[medium] 78 Subset (backtracking shall be supplemented later)
JVM foundation > CMS garbage collector
[image denoising] image denoising based on trilateral filter with matlab code
【LeetCode】5. 最长回文子串
Can tonghuashun open an account? Is it safe to open an account in tonghuashun? How to open a securities account
[probability theory and mathematical statistics] final review: formula summary and simple examples (end)
Ansible playbook and variable (II)
The kotlin coroutine -- coroutine context and exception propagation
Economist focuses on WTO MC12: digital economy may become an important issue
Wechat applet withdrawal function
(downloadable) Research Report on the development and utilization of government data (2021), a glimpse of the development of Government Office
Photoshop:PS如何实现放大图片不模糊