当前位置:网站首页>【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
边栏推荐
- Logstash timestamp converted to UNIX nanosecond nano second time
- 设计消息队列存储消息数据的 MySQL 表格
- vim利用右下4键
- JVM foundation > G1 garbage collector
- Preliminary use of jvisualvm
- Prefix sum and difference
- Es6+ new content
- Is it safe to open an account in tonghuashun? How to open an account for securities
- [sword finger offer simple] sword finger offer 24 Reverse linked list
- 【LeetCode】102. 二叉树的层序遍历
猜你喜欢

接口测试工具apipost3.0版本对于流程测试和引用参数变量

Configuring Dingding notification of SQL audit platform archery

2021 rust survey results released: 9354 questionnaires collected

Photoshop:PS如何实现放大图片不模糊

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

Jin AI her power | impact tech, she can

NoSQL - redis configuration and optimization (II) high availability, persistence and performance management

(downloadable) Research Report on the development and utilization of government data (2021), a glimpse of the development of Government Office

Yyds dry inventory insider news: Series high-frequency interview questions, worth a visit!

Database daily question --- day 10: combine two tables
随机推荐
MySQL architecture and basic management (II)
[simple] 155 Minimum stack
A puzzle about + =
2022-02-28 incluxdb high availability planning
leetcodeSQL:574. Elected
List of open source alternative projects of world famous Cloud Service SaaS companies
JVM Basics - > how to troubleshoot JVM problems in your project
JVM foundation > G1 garbage collector
"Oracle database parallel execution" technical white paper reading notes
Role of volatile keyword
Qt Quick 3D学习:鼠标拾取物体
数据库每日一题---第10天:组合两个表
Why is pain rating important?
How to abstract a problem into a 0-1 knapsack problem in dynamic programming
My struggle: my years in foreign enterprises (1)
Preliminary use of jvisualvm
[machine learning] learning notes 01- introduction
Palindrome linked list and linked list intersection problem (intersecting with Xinyi people) do you really know?
Create a virtual thread using loom - David
Unity 常用3D数学计算