"""
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.
"""
"""
滑动窗口,与leetcode713类似
"""
class Solution:
def minSubArrayLen(self, s: int, nums):
_sum = 0
res = len(nums) + 1
i = 0
for j in range(len(nums)):
_sum += nums[j]
while _sum >= s: #!!!
res = min(res, j-i+1)
_sum -= nums[i]
i += 1
return res if res <= len(nums) else 0

leetcode209 Minimum Size Subarray Sum的更多相关文章

  1. [LintCode] Minimum Size Subarray Sum 最小子数组和的大小

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

  2. leetcode面试准备:Minimum Size Subarray Sum

    leetcode面试准备:Minimum Size Subarray Sum 1 题目 Given an array of n positive integers and a positive int ...

  3. [LeetCode] Minimum Size Subarray Sum 解题思路

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

  4. 领扣-209 长度最小的子数组 Minimum Size Subarray Sum MD

    Markdown版本笔记 我的GitHub首页 我的博客 我的微信 我的邮箱 MyAndroidBlogs baiqiantao baiqiantao bqt20094 baiqiantao@sina ...

  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] Minimum Size Subarray Sum 最短子数组之和

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

  7. Minimum Size Subarray Sum LT209

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

  8. 和大于S的最小子数组 · Minimum Size Subarray Sum

    [抄题]: 给定一个由 n 个正整数组成的数组和一个正整数 s ,请找出该数组中满足其和 ≥ s 的最小长度子数组.如果无解,则返回 -1. 给定数组 [2,3,1,2,4,3] 和 s = 7, 子 ...

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

随机推荐

  1. GTA5整合包

    链接:https://pan.baidu.com/s/1WUvLMyTcQYsw3wi6OfJfJA 提取码:jcpm

  2. MYSQL 传汉字获取拼音首字母

    --获取单个汉字首字母拼音 --CREATE DEFINER=`by`@`%` FUNCTION `fun_first_pinyin`(`P_NAME` VARCHAR(5)) RETURNS var ...

  3. 对图书管理系统5W1H的分析

    5W1H 对图书管理系统的分析 1.Who 学生.老师和图书馆管理员 2.When 借还书的时候使用,还有用户流量的统计在每天晚上都会看一下有多少人哪些人看了的,基本绝大多数时间都可以看 3.Wher ...

  4. AtCoDeer and Election Report

    问题 G: AtCoDeer and Election Report 时间限制: 1 Sec  内存限制: 128 MB[提交] [状态] 题目描述 AtCoDeer the deer is seei ...

  5. 鸡汤 - Choice is yours

    传送门 https://kamranahmed.info/blog/2018/03/24/choice-is-yours/ Our whole lives are driven by the choi ...

  6. VBA 学习笔记 - 消息框

    学习资料:https://www.yiibai.com/vba/vba_macro_comments.html 注释 单引号或 REM 开头 丸子:多行注释咋办? 消息框(MsgBox) 函数功能:显 ...

  7. CSS:定位概述

    background-position 背景定位 如果,说浮动, 关键在一个 "浮" 字上面, 那么 我们的定位,关键在于一个 "位" 上. PS: 定位是我们 ...

  8. Spring Boot 中使用 HttpClient 进行 POST GET PUT DELETE

    有的时候,我们的 Spring Boot 应用需要调用第三方接口,这个接口可能是 Http协议.可能是 WebService.可能是 FTP或其他格式,本章讨论 Http 接口的调用. 通常基于 Ht ...

  9. 吴裕雄--天生自然Numpy库学习笔记:NumPy 高级索引

    import numpy as np x = np.array([[1, 2], [3, 4], [5, 6]]) y = x[[0,1,2], [0,1,0]] print (y) import n ...

  10. 关于永久POE

    1.传统POE 在我们的企业网络中,经常会使用交换机给IP电话或者无线AP供电,以使得其正常的工作. 正常情况下,我们都知道,普通的POE是在PSE交换机启动完成后,然后再给PD(Power Devi ...