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. Codeforces 758C-Unfair Poll

    Unfair Poll time limit per test 1 second memory limit per test 256 megabytes input standard input ou ...

  2. I - The 3n + 1 problem(2.4.2)

    I - The 3n + 1 problem(2.4.2) Crawling in process... Crawling failed Time Limit:1000MS     Memory Li ...

  3. Nginx作为TCP负载均衡

    参考文档:https://www.cnblogs.com/stimlee/p/6243055.html Nginx在1.9版本以后支持TCP负载均衡,模块默认是没有编译的,需要编译时添加—with-s ...

  4. [No0000B5]C# 类型基础 值类型和引用类型 及其 对象判等 深入研究1

    引言 本文之初的目的是讲述设计模式中的 Prototype(原型)模式,但是如果想较清楚地弄明白这个模式,需要了解对象克隆(Object Clone),Clone其实也就是对象复制.复制又分为了浅度复 ...

  5. 添加字体与字符集locale支持(基于busybox文件系统)

    添加字体与字符集(基于busybox文件系统) 2011-10-11 14:07:32 分类: LINUX 1.添加字体 下面以文泉驿为例 $ mkdir  -p  rootfs/usr/share/ ...

  6. PHP之二维数组根据某个下标排序

    function arraySortByElements($array2sort,$sortField,$order,$iscount=false) { $functionString=' if (' ...

  7. HTTP协议之Transfer-Encoding

    HTTP协议中的Transfer-Encoding 浏览器和服务器端支持持久连接 持久连接(Persist Connection) HTTP1.0默认不是持久连接的 HTTP1.1默认是持久连接的 在 ...

  8. android&sqlsever

    http://blog.csdn.net/chaoyu168/article/details/51910601

  9. c# http get post转义HttpUtility.UrlEncode

    //该数据如果要http get.post提交,需要经过转义,否则该数据中含& ''等字符会导致意外错误.需要转义.这里用HttpUtility.UrlEncode来转义.接收方无需反解析 s ...

  10. Runloop的再学习之浅析(一)

    一,认识RunLoop 我的理解: 1. 在编程的世界里,万物皆对象.所以RunLoop 实际上也是一个对象,这个对象管理了其需要 处理的事件和消息,并提供了一个入口函数来执行上面 Event Loo ...