`
随便小屋
  • 浏览: 102680 次
  • 性别: Icon_minigender_1
  • 来自: 大连
社区版块
存档分类
最新评论

LeetCode-209-Minimum Size Subarray Sum

阅读更多

Minimum Size Subarray Sum

 

来自 <https://leetcode.com/problems/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.

More practice:

If you have figured out the O(n) solution, try coding another solution of which the time complexity is O(n log n).

Credits:

Special thanks to @Freezen for adding this problem and creating all test cases.

题目解读:

给定一个长度为n的正整数数组和和一个正整数,找出一个长度最短的子数组,使子数组元素之和sum  s。如果这样的子数组不存在,则返回0.

例如:给定的数组为[2,3,1,2,4,3] s=7则子数组[4,3]的长度最小。

解析:

 

解法一:从前向后遍历数组,设置两个指针i,j ,在开始时两个指针指向相同的位置。遍历数组时,一个指针i首先保持不动,另外一个指针j向后移动并记录并记录指针ij之间数组元素的和sum,如果sum<s,则j继续向后移动。如果sum>=s。则记录length=j-i,看其是否小于minlength, 如果小于,则minlength=length;否则minlength保持不变。此时ij再同时指向i的下一个位置,依次循环。主要利用滑动窗口的思想。



 
图片摘自:http://blog.csdn.net/lisonglisonglisong/article/details/45666975

解法二:

摘自http://www.cnblogs.com/tonyluis/p/4564076.html

在方法一中,保持前面的指针不变,让后边的指针移动。在方法二中,维护一个左边界,让右边的正常移动。

解法三:分治法

摘自http://www.huhaoyu.com/leetcode-minimum-size-subarray-sum/

 

解法一代码:

Public int minSubArrayLen(int s, int[] nums) {
		int sum = 0;
		int minlength = nums.length;
		int length = 0;
		for(int i=0; i<nums.length; i++) {
			sum += nums[i];
			int j=0;
			for(j=i+1; j<nums.length; j++ ) {
				if(sum >= s) {
					length = j-i;
					break;
					
				} else {
					sum += nums[j];
				}
			}
			if(j==nums.length && sum >= s) {
				length = j-i;
			}
			if(minlength > length) {
				minlength = length;
			}
			sum = 0;
		}
		if(minlength != nums.length) {
			return minlength;
		} else {
			return 0;
		}
    }

 

 

解法一性能:



 

 

解法二代码:

    public int minSubArrayLen(int s, int[] nums) {
        	int i=0;
		int left = 0;
		int minLength = nums.length;
		int sum = 0;
		int length = 0;
		for(i=0; i<nums.length; i++) {
			sum += nums[i];
			while(sum >= s) {
			    length = i-left+1;
			    //minLength = Math.min(minLength, i-left+1);
			    if(minLength > length) {
			        minLength = length;
			    }
				sum -= nums[left++];
			}
		}
		
		//return minLength==nums.length?0:minLength;
		if(minLength == nums.length) {
		    return 0;
		} else {
		    return minLength;
		}
    }

 

 

解法二性能:



 

 

解法三代码:(仅作参考,提交不成功,但是个很好的思想)

class Solution {
public:
    int MAX_INT = 999999999;
    int minSubArrayLen(int s, vector<int>& nums) {
        if (!nums.size()) return 0;
        return findMinLen(s, nums, 0, nums.size() - 1);
    }
    int findMinLen(int s, vector<int>& nums, int bottom, int top) {
        if (top == bottom) return nums[top] >= s ? 1 : 0;
        int mid = (bottom + top) / 2;
        int left = mid, right = mid + 1, sum = 0, len;
        while (sum < s && (right <= top || left >= bottom)) {
            if  (right > top) {
                sum += nums[left]; --left;
            }
            else if (left < bottom) {
                sum += nums[right]; ++right;
            }
            else if (nums[left] > nums[right]) {
                sum += nums[left]; --left;
            }
            else {
                sum += nums[right]; ++right;
            }
        }
        if (sum >= s) {
            len = right - left - 1;
            int leftLen = findMinLen(s, nums, bottom, mid);
            int rightLen = findMinLen(s, nums, mid + 1, top);
            return minValue(leftLen, rightLen, len);
        }
        else {
            return 0;
        }
    }
    int minValue(int x, int y, int z) {
        if (!x) x = MAX_INT;
        if (!y) y = MAX_INT;
        if (x <= y && x <= z) return x;
        if (y <= x && y <= z) return y;
        return z;
    }
};

 来自 <http://www.huhaoyu.com/leetcode-minimum-size-subarray-sum/>

 

  • 大小: 2.2 KB
  • 大小: 20 KB
  • 大小: 19.8 KB
分享到:
评论

相关推荐

    leetcode209-LeetCode209_Minimum_Size_Subarray_Sum:给定一个整型数组和一个数字s,找到数组中最

    LeetCode209_Minimum_Size_Subarray_Sum 给定一个整型数组和一个数字s,找到数组中最短的一个连续子数组, 使得连续子数组的数字和sum&gt;=s,返回这个最短的连续子数组的长度值, 如:给定数组[2,3,1,2,4,3],s=7 答案为...

    leetcode分类-leetcode:leetcode

    leetcode 分类 Leetcode Introduction 本文旨在将leetcode的题型分类 Pattern: Sliding window 滑动窗口类型的题目经常是用来执行数组或是链表上某个区间(窗口)上的操作。比如找最长的全为1的子数组长度。滑动窗口...

    leetcode530-alogritme-interview:alogritme-面试

    leetcode 530 ** 面试leetcode题目 ** python ** 第一章 数组基础 ** python 1-1 BinarySearch 1-2 即使简单的问题,也有很多优化的思路 283 27 26 80 1-3 三路快排partition思路的应用 Sort Color 75 88 215 1-4 ...

    leetcode530-Play-with-Algorithms:基本的算法和数据结构

    leetcode 530 Play-with-Algorithms 基本的算法和数据结构 来自liuyubobobo老师的课程 章节 讲解例题 课程练习题 更多扩展练习 难题推荐 第一章 算法面试到底是什么鬼? [无] [无] 第二章 面试中的复杂度分析 [无] ...

    LeetCode最全代码

    371 | [Sum of Two Integers](https://leetcode.com/problems/sum-of-two-integers/) | [C++](./C++/sum-of-two-integers.cpp) [Python](./Python/sum-of-two-integers.py) | _O(1)_ | _O(1)_ | Easy | LintCode | ...

    javalruleetcode-LeetCode::lollipop:个人LeetCode习题解答仓库-多语言

    java lru leetcode :ice_cream: LeetCode Kindem 的个人 LeetCode 题解仓库,欢迎交流学习。 下面的目录中 ...LeetCode ...LeetCode ...Subarray 55 Jump Game 56 Merge Intervals 64 Minimum Path Sum 73

    word源码java-Play-with-Algorithm-Interview-Learningnotes:Play-with-Algori

    在LeetCode上解决第一个问题 Move Zeros 3-4 即使简单的问题,也有很多优化的思路 3-5 三路快排partition思路的应用 Sort Color 3-6 对撞指针 Two Sum II - Input Array is Sorted 3-7 滑动窗口 Minimum Size ...

    leetcode分类-LeetCode:力码

    leetcode 分类 LeetCode Progress 128/154 Other Solutions C++,有详细思路解释 python,部分有解释 Java,部分有解释 Java Associated Documents and Resources Peter norvig神牛Python代码写的很飘逸,果然是有LISP...

    leetcode中国-Final450_Data-Structures:Final450_数据结构

    leetcode中国Final450_数据结构 实际上,这个存储库包含成为优秀竞争程序员最重要的数据结构和算法。 他们的名字是: Questions ----------- *Reverse the array *Find the maximum and minimum element in an array...

    leetcode中国-DP:DP

    leetcode中国大批 0. Count Prime 1. Reverse the array 2. Find the maximum and minimum element in an array 3. Find the "Kth" max and min element of an array 4. Given an array which consists of only 0, 1...

    gasstationleetcode-leetcode:LeetcodeOJ解决方案

    leetcode 【演示记录】 报告 展示 2017/03/06 1.二和,167.二和二 2107/03/06 15.3 总和,16.3 总和最近,18.4 总和,11.最多水的容器 2017/03/09 62.Unique Paths, 63.Unique Paths II, 64.Minimum Path Sum 2017/...

    cpp-算法精粹

    Minimum Path Sum Edit Distance Decode Ways Distinct Subsequences Word Break Word Break II Dungeon Game House Robber House Robber II House Robber III Range Sum Query - Immutable Range Sum Query 2D - ...

Global site tag (gtag.js) - Google Analytics