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.
Follow up:
If you have figured out the O(n) solution, try coding another solution of which the time complexity is O(n log n). 
 
 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的更多相关文章

  1. [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 ...

  2. 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 ...

  3. LeetCode 209 Minimum Size Subarray Sum

    Problem: Given an array of n positive integers and a positive integer s, find the minimal length of ...

  4. 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 ...

  5. 【刷题-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 ...

  6. 【LeetCode】209. Minimum Size Subarray Sum 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址: https://leetcode.com/problems/minimum- ...

  7. 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 ...

  8. 【Leetcode】209. Minimum Size Subarray Sum

    Question: Given an array of n positive integers and a positive integer s, find the minimal length of ...

  9. 【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 ...

随机推荐

  1. Adobe Photoshop CS6简单的破解

    由于网站的页面布局和素材准备等等需要用到Photoshop,所以下载了个 Photoshop CS6,写这份破解文档的大佬感觉写的很复杂,看了让人头疼,乱搞中突然发现一个方法可以很快的进行破解操作,我 ...

  2. python web篇 创建数据库

    python3 manage.py migrate ls sqlite3 使用单文件数据库,管理方便 运行测试 python manage.py runserver 输入http://127.0.0. ...

  3. Codeforces 670E - Correct Bracket Sequence Editor - [线段树]

    题目链接:https://codeforces.com/contest/670/problem/E 题意: 给出一个已经匹配的括号串,给出起始的光标位置(光标总是指向某个括号). 有如下操作: 1.往 ...

  4. MyBatis中choose when正确写法

    <choose> <when test="scoreRange!=null and scoreRange eq 1"> AND sc.score <! ...

  5. tomcat远程部署war包,显示连接被重置

    在tomcat 目录: /opt/apache-tomcat-9.0.13/webapps/manager/WEB-INF/web.xml 下修改: <multipart-config> ...

  6. Linux 配置SFTP,配置用户访问权限

    之前我服务器是使用的Windows Server 2003,这段时间由于访问量变大我还是机智的换成Linux了,在搭建FTP的时候看到网上都是推荐vsftpd,不过我不推荐这个家伙,看官且看下文. 我 ...

  7. 随着应用对事务完整性和并发性要求的不断提高,MySQL才开始开发基于事务的存储引擎

    MYSQL 解锁与锁表 - 专注it - 博客园 https://www.cnblogs.com/wanghuaijun/p/5949934.html 2016-10-11 16:50 MYSQL 解 ...

  8. [daily] socks代理转化为http代理

    我用SS爬梯子,它是socks5的代理,在电脑上. 很长时间以来,我的手机是不能出去的.那么我该怎么弄才能让手机也出去呢.最简单的办法是让手机也ss. 但问题是,怎么给手机装上一个ss. 1.  用电 ...

  9. python fabric实现远程操作和部署示例

    https://www.jb51.net/article/48434.htm 近期接手越来越多的东西,发布和运维的工作相当机械,加上频率还蛮高,导致时间浪费还是优点多.修复bug什么的,测试,提交版本 ...

  10. LeetCode 888 Fair Candy Swap 解题报告

    题目要求 Alice and Bob have candy bars of different sizes: A[i] is the size of the i-th bar of candy tha ...