[LeetCode] 209. Minimum Size Subarray Sum_Medium
Given an array of n positive integers and a positive integer s, find the minimal length of a contiguous subarray of which the sum ≥ s. If there isn't one, return 0 instead.
Example:
Input:s = 7, nums = [2,3,1,2,4,3]
Output: 2
Explanation: the subarray[4,3]
has the minimal length under the problem constraint.
class Solution:
def minSubArrayLen(self, mins, nums):
"""
:type s: int
:type nums: List[int]
:rtype: int
"""
if not nums: return 0
l, r, s, m = 0, 0, 0, None
while r < len(nums):
s += nums[r]
r += 1
while s >= mins:
m = r - l if not m else min(m, r-l)
s -= nums[l]
l += 1
return m if m else 0
我自己最开始的solution想的是用Two Pointers, l = 0, r = len(nums) -1, 然后分别往中间扫, 但是pass不了所有的test cases, 不知道问题出在哪.
# myself solution, but can not pass all the test cases if not nums: return 0
l, r, ans = 0, len(nums)-1, 0
while l<= r and sum(nums[l:r+1]) >= s:
ans = r-l +1
#print(nums[l:r+1])
if nums[l] < nums[r]:
l += 1
elif nums[l] > nums[r]:
r -= 1
else:
templ, tempr = l+1, r-1
condition = True
while(templ <= tempr and condition):
if nums[templ] == nums[tempr]:
templ += 1
tempr -= 1
elif nums[templ] < nums[tempr]:
l += 1
condition = False
else:
r -= 1
condition = False
if condition:
l += 1
return ans
[LeetCode] 209. Minimum Size Subarray Sum_Medium的更多相关文章
- [LeetCode] 209. Minimum Size Subarray Sum 最短子数组之和
Given an array of n positive integers and a positive integer s, find the minimal length of a contigu ...
- LeetCode 209. Minimum Size Subarray Sum (最短子数组之和)
Given an array of n positive integers and a positive integer s, find the minimal length of a contigu ...
- LeetCode 209 Minimum Size Subarray Sum
Problem: Given an array of n positive integers and a positive integer s, find the minimal length of ...
- Java for LeetCode 209 Minimum Size Subarray Sum
Given an array of n positive integers and a positive integer s, find the minimal length of a subarra ...
- 【刷题-LeetCode】209. Minimum Size Subarray Sum
Minimum Size Subarray Sum Given an array of n positive integers and a positive integer s, find the m ...
- 【LeetCode】209. Minimum Size Subarray Sum 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址: https://leetcode.com/problems/minimum- ...
- LeetCode OJ 209. Minimum Size Subarray Sum
Given an array of n positive integers and a positive integer s, find the minimal length of a subarra ...
- 【Leetcode】209. Minimum Size Subarray Sum
Question: Given an array of n positive integers and a positive integer s, find the minimal length of ...
- 【leetcode】Minimum Size Subarray Sum(middle)
Given an array of n positive integers and a positive integer s, find the minimal length of a subarra ...
随机推荐
- CCPC-Wannafly Winter Camp Day4 G---置置置换【递推】【组合数】【逆元】
置置置换 已经提交 已经通过 63.89% Total Submission:72 Total Accepted:46 题目描述 wlswlswls有一个整数nnn,他想请你算一下有多少1...n1. ...
- 泡泡一分钟:Motion Planning for a Small Aerobatic Fixed-Wing Unmanned Aerial Vehicle
Motion Planning for a Small Aerobatic Fixed-Wing Unmanned Aerial Vehicle Joshua Levin, Aditya Paranj ...
- python字典查询功能
def fetch(data): print(' 查询功能') print('用户数据是',data) def add(): pass def change(): pass def delete(): ...
- 关于Dosbox0.74无法使用masm命令
今天尝试在dosbox里编译asm源代码文件 但是提示“illegal command”,也就是非法命令 开始还以为我的dosbox版本不对 但是去网上查阅资料发现别人用这个版本都可以使用 所以百思不 ...
- DOM节点的基础操作
1.寻找节点 //寻找节点 id方法 document.getElementById(); //标准 //寻找节点层次方法 parentNode().firstChild()和lastChild(): ...
- [knowledge] 停止等待协议
再读TCP/IP详解 说到流量控制, 可能便涉及了两方面 1. 停止等待协议. https://baike.baidu.com/item/%E5%81%9C%E6%AD%A2%E7%AD%89%E5% ...
- [security][modsecurity] modsecurity 规则说明/中文/转发
原文转发以防丢失. 地址: http://www.catssec.com:8090/exploit/?p=691 转来细读之后,并没有太多的参考价值 :( modsecurity规则手册 通用格式 ...
- WordCount 的实现与测试
一.开头 (1)合作者:201631062627,201631062427 (2)代码地址:https://gitee.com/catchcatcat/WordCount.git 二.正文 (1)基本 ...
- 转:ActiveMQ的作用总结(应用场景及优势)
原文地址: ActiveMQ的作用总结(应用场景及优势) 业务场景说明: 消息队列在大型电子商务类网站,如京东.淘宝.去哪儿等网站有着深入的应用, 队列的主要作用是消除高并发访问高峰,加快网站的响应速 ...
- LeetCode 897 Increasing Order Search Tree 解题报告
题目要求 Given a tree, rearrange the tree in in-order so that the leftmost node in the tree is now the r ...