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. go 的文件处理

    准备一个文件 imooc.txt hello world! 一.使用 io/ioutil 包 定义一个 check 函数 func check(err error) { if err != nil { ...

  2. 【Vue】---编写Vue插件流程---【巷子】

    一.在Vue中编写插件流程 1.创建组件 components/message.vue <template> <div class="message" v-if= ...

  3. 初学的linux命令行

    这几条命令是今天刚初学的,以前总看别人输入命令,好利落,到自己了,真心觉得难.目前就学了这几个命令.后期等学会了,再进行添加 vm 文件名 ——> 新建文件: :wq  ——>  保存并退 ...

  4. [No0000B8]WPF或Winform调用系统Console控制台显示信息

    using System; using System.Collections.Generic; using System.Diagnostics; using System.IO; using Sys ...

  5. 基于cdh5.10.x hadoop版本的apache源码编译安装spark

    参考文档:http://spark.apache.org/docs/1.6.0/building-spark.html spark安装需要选择源码编译方式进行安装部署,cdh5.10.0提供默认的二进 ...

  6. qs.parse()、qs.stringify()使用方法

    qs是一个npm仓库所管理的包,可通过npm install qs命令进行安装. 1. qs.parse()将URL解析成对象的形式 const Qs = require('qs'); let url ...

  7. 《Linux 文本处理》- awk 分析 nginx 日志

    废话不多说,简单了解一下 awk - 强大的文本分析工具,也就是分析日志 最常用的就是分析日志了吧,做统计什么,这里也拿 nginx 日志来做分析 1:统计出现次数 - 比如状态码出现次数 - 先直接 ...

  8. java JDBC (四)

    package cn.sasa.demo4; import java.sql.Connection; import java.sql.PreparedStatement; import java.sq ...

  9. java 大数据运算 BigInteger BigDecimal

    package cn.sasa.demo5; import java.math.BigDecimal; import java.math.BigInteger; public class BigDat ...

  10. php获取数据库结构

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...