当前位置: 首页 > news >正文

LeetCode -- Minimum Size Subarray Sum

题目描述:


Given an array of n positive integers and a positive integer s, find the minimal length of a subarray of which the sum ≥ s. If there isn't one, return 0 instead.


For example, given the array [2,3,1,2,4,3] and s = 7,
the subarray [4,3] has the minimal length under the problem constraint.


对于数组A中的子数组a,找到满足Sum(a[0]+a[1]...a[n-1]) >= target时的最短长度的子数组。


思路:
本题属于移动窗口问题,最直接的办法就是分别以a[i]为起点,不断往后移动指针进行累加存到s,当s>=target时,更新最短长度min。
时间复杂度为O(N^2)


实现代码:


public class Solution {
    public int MinSubArrayLen(int s, int[] nums) 
    {
       	int? min = null;
    	
    	for(var i = 0;i < nums.Length; i++){
    		var l = MinLen(nums, i, s);
    		if(l.HasValue && l < min || min == null){
    			min = l;
    		}
    	}
    	
    	return min.HasValue ? min.Value : 0;
    }
    
    private int? MinLen(int[] nums, int start, int target)
    {
    	var s = 0;
    	var startFrom = start;
    	while(start < nums.Length){
    		s += nums[start];
    		if(s >= target){
    			return (start - startFrom + 1);
    		}
    		start ++;
    	}
    	
    	return null;
    }
    
}



本题使用two pointer可以完成o(N)的实现。 比较推荐这种实现方式。


1. 使用left和right两个指针分别从0作为起点
2. 如果当前s小于target,right一直往后走,直到s大于或等于target
3. 如果s大于等于target,left一直往后走,同时判断left与right的距离,更新最小窗口的大小。


本实现方式参考了连接:
http://www.geeksforgeeks.org/minimum-length-subarray-sum-greater-given-value/




实现代码:

public class Solution {
    public int MinSubArrayLen(int s, int[] nums) 
    {
       	var len = nums.Length;
    	var sum = 0;
    	int? min = null;
    	
     	// use two pointers 
        var start = 0;
    	var end = 0;
    	
        while (end < len)
        {
    		// if current sum if smaller , keep moving right pointer forward
            while (sum < s && end < len){
                sum += nums[end];
    			end ++;
    		}
     		
    		// if sum is more than target, keep moving left pointer forward to shrink the window size
            while (sum >= s)
            {
    			// find a smaller window then update size
                if (!min.HasValue || end - start < min){
    				min = end - start;
    			}
    			
    			// unload left most value from sum
                sum -= nums[start];
    			start ++;
            }
    		
    		// now sum less than target again
    		// lets play again with the right pointer if not yet reach the end
        }
    	
        return !min.HasValue ? 0 : min.Value;
    }
    
    
}




相关文章:

  • LeetCode -- Number of 1 Bits
  • 对象属性拷贝(全匹配拷贝)
  • LeetCode -- Reorder List
  • 最近
  • LeetCode -- Search a 2D Matrix II
  • [IE编程] 打开/关闭IE8的光标浏览模式(Caret Browsing)
  • LeetCode -- 3Sum Closest
  • 使用反射将业务对象绑定到 ASP.NET 窗体控件
  • LeetCode -- 4Sum
  • LeetCode -- Binary Tree Level Order Traversal II
  • (ZT)一个美国文科博士的YardLife
  • LeetCode -- Clone Graph
  • Oracle 中的nvl() 函数 相当于Sql Server 的 isnull()
  • LeetCode -- Combinations
  • [IE编程] WebBrowser控件中设置页面的缩放
  • “寒冬”下的金三银四跳槽季来了,帮你客观分析一下局面
  • 《微软的软件测试之道》成书始末、出版宣告、补充致谢名单及相关信息
  • 【剑指offer】让抽象问题具体化
  • 2018天猫双11|这就是阿里云!不止有新技术,更有温暖的社会力量
  • Java 9 被无情抛弃,Java 8 直接升级到 Java 10!!
  • python3 使用 asyncio 代替线程
  • React组件设计模式(一)
  • 翻译:Hystrix - How To Use
  • 搞机器学习要哪些技能
  • 盘点那些不知名却常用的 Git 操作
  • 数据科学 第 3 章 11 字符串处理
  • 学习JavaScript数据结构与算法 — 树
  • ​Python 3 新特性:类型注解
  • #LLM入门|Prompt#2.3_对查询任务进行分类|意图分析_Classification
  • #ubuntu# #git# repository git config --global --add safe.directory
  • #大学#套接字
  • (DenseNet)Densely Connected Convolutional Networks--Gao Huang
  • (附源码)python旅游推荐系统 毕业设计 250623
  • (四)图像的%2线性拉伸
  • (转)shell中括号的特殊用法 linux if多条件判断
  • *Django中的Ajax 纯js的书写样式1
  • .java 指数平滑_转载:二次指数平滑法求预测值的Java代码
  • .NET CORE 第一节 创建基本的 asp.net core
  • .NET 设计模式初探
  • .NET/C# 使用反射注册事件
  • .Net6支持的操作系统版本(.net8已来,你还在用.netframework4.5吗)
  • .NetCore 如何动态路由
  • .NET处理HTTP请求
  • .NET导入Excel数据
  • .net中调用windows performance记录性能信息
  • @Autowired注解的实现原理
  • @TableId注解详细介绍 mybaits 实体类主键注解
  • [ CTF ] WriteUp- 2022年第三届“网鼎杯”网络安全大赛(朱雀组)
  • [2021]Zookeeper getAcl命令未授权访问漏洞概述与解决
  • [52PJ] Java面向对象笔记(转自52 1510988116)
  • [C++ 从入门到精通] 12.重载运算符、赋值运算符重载、析构函数
  • [C++] 统计程序耗时
  • [Excel] vlookup函数
  • [halcon案例2] 足球场的提取和射影变换
  • [iphone-cocos2d]关于Loading的若干处理和讨论