[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 ...
随机推荐
- AJAX返回总是ERROR或是没有数据的问题
如果总是到ERROR,是因为async没有定义为false,设置为同步,数据类型要设置为text,不要用json. 示例: if (IDcard != "") { $.ajax({ ...
- 2018GIAC全球互联网架构大会上海站最新日程抢先看!
2018年11月23-24日,为期两天的 GIAC全球互联网架构师大会将在上海拉开帷幕.GIAC全球互联网架构大会是由msup和高可用架构技术社区联合举办的面向架构师.技术负责人及高端技术从业人员的年 ...
- 如何查看目前正在使用的Windows10是哪个版本?
其实相当的简单: win+R 输入winver,就会出现如下图的信息: 就能看到版本信息了
- Datatables js 复杂表头 合并单元格
x →Datatables官网← x 项目中用到的Table都是用Datatables插件来搞得: 以前都是生成一般性的table: 近期要生成一些复杂表头,合并单元格之类的: 研究了一下. x 去官 ...
- [No0000FF]鸡蛋煮熟了蛋黄为什么发黑?
你是否发现,鸡蛋煮熟后,蛋黄表面会呈现青黑色. 这是怎么回事? 这是因为鸡蛋的蛋白质富含有半胱氨酸,鸡蛋如果加热过度使半胱氨酸部分分解产生硫化氢,与蛋黄中的铁结合形成黑色的硫化铁.煮蛋中如果鸡蛋表面的 ...
- beginner’s mistake
PHP Advanced and Object-Oriented Programming 3rd Edition Related to modularity is abstraction: class ...
- python的一些基本概念知识和面试题
对于机器学习算法工程师而言,Python是不可或缺的语言,它的优美与简洁令人无法自拔.那么你了解过Python编程面试题吗?从Python基础到网页爬虫你是否能全方位Hold住?今天,机器之心为读者们 ...
- 分析占用了大量 CPU 处理时间的是Java 进程中哪个线程
下面是详细步骤: 1. 首先确定进程的 ID ,可以使用 jps -v 或者 top 命令直接查看 2. 查看该进程中哪个线程占用大量 CPU,执行 top -H -p [PID] 结果如下: 可以发 ...
- delphi inttohex 整型到十六进制
inttohex from delphi help: Returns the hex representation of an integer. Unit SysUtils Category nume ...
- 操作系统->数组越界(待完善)
工作中无意间发现了一段可能存在数组越界的代码, 就在本地仿者写了一段越界的小程序, 先记录下,待以后看操作系统知识的时候,再深入分析 #include <stdio.h> #include ...