1. 程式人生 > >Minimum Size Subarray Sum-最小長度字陣列和問題

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. <p>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.</p> <p>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).</p>

  • 問題分析:剛開始看到這個題時,並沒有考慮到字陣列連續的問題,發現只能用遞迴的解法,複雜度很高,後來搜了下,發現其實是自己理解的問題,這個題必須考慮到連續的問題。
  • Solution 1: 這樣O(n)的解法就很明顯了,滑動視窗,兩個指標即可解決。對陣列中的每個數(左指標),計算從它開始往右的和,直到不小於s為止(右指標),然後更新這個長度,隨後左指標右移,直至從當前左指標到右指標的和小於s為止。此外,這個題可能發生整個陣列和小於s的情況,所以只需要在minLen的初始值設定為大於nums.legth,最後在發揮至的時候判斷下即可解決。
  • For example:對當前例子中的陣列,left = 0, right = 3.即從2到下一個2為止這個字陣列和(8)是超出s(8),然後left右移, 發現left=1時,字陣列和變味了8-2=6,所以 從當前的左指標開始,右指標開始移動.這樣不斷更新這個值即可。程式碼如下:
    public class Solution {
        public int minSubArrayLen(int s, int[] nums) {
            if (nums == null || s <= 0)
                return 0;
           //滑動視窗
           int left = 0, right = 0, len = nums.length + 1, temp = 0;
           while(right < nums.length){
               while(right < nums.length && temp < s){
                   temp += nums[right++];
               }
               while(temp >= s){
                    len = Math.min(len, right - left);
                    temp -= nums[left++];
               }
           }
           return len == nums.length + 1? 0 : len;
        }
    }


  • Solution 2:more practice要求給出O(nlogn)的解法,實在是很不明白,這個題O(n)的解法已經非常清晰明瞭,為何還要給出複雜度高的解法,可既然是題目要求,那就嘗試接下去,看到log(n),首先就想到了二分查詢,可是如何利用二分查詢,因為二分查詢要求查詢的陣列有序,那就讓陣列變得有序即可。讓陣列變得有序秩序再申請一個數組sums, 其中sums[i] 代表nums[0...i-1]的和。這樣對麼個sums[i],計算右邊界即可。右邊界是sums[high] >sums[low] + s的值。其實從這既可以看出,這有解法一併沒有本質的區別.另外,這個解法還要注意,一旦sums[low] + s超出了sums[sums.length-1],即說明後邊不肯能有滿足條件的解了,停止搜尋.程式碼如下:
    public class Solution {
        public int minSubArrayLen(int s, int[] nums) {
            if (nums == null || s <= 0)
                return 0;
            int[] sums = new int[nums.length + 1];
            for(int i = 1; i <= nums.length; i++){
                sums[i] = sums[i-1] + nums[i-1];
            }
            if(sums[sums.length - 1] < s)
                return 0;
            int len = sums.length,low, high, target, mid;
            for(int i = 0; i < sums.length; i++){
                low = i+ 1;high = sums.length-1; target = sums[i] + s;
                while(low <= high){
                    mid = low + ((high - low) >> 1);
                    if(sums[mid] >= target)
                        high = mid - 1;
                    else if (sums[mid] < target)
                        low = mid + 1;
                }
                if(low >= sums.length)
                    break;
                if(low - i < len)
                    len = low - i;
            }
            return len;
        }
    }


    此外,本文的解法在http://www.cnblogs.com/grandyang/p/4501934.html的基礎上而來,特此宣告。

相關推薦

Minimum Size Subarray Sum-長度陣列問題

問題描述:Given an array of n positive integers and a positive integer s, find the minimal length of a

[LeetCode] 209. Minimum Size Subarray Sum 短子數組之和

xpl d+ min sliding HERE sel n) null pac Given an array of n positive integers and a positive integer s, find the minimal length of a cont

[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

LeetCode 209. 長度的子陣列 Minimum Size Subarray Sum

3-7 滑動視窗 Minimum Size Subarray Sum 題目:LeetCode 209. 長度最小的子陣列 給定一個含有 n 個正整數的陣列和一個正整數 s ,找出該陣列中滿足其和 ≥ s

領釦-209 長度的子陣列 Minimum Size Subarray Sum MD

目錄 長度最小的子陣列 Minimum Size Subarray Sum -209 問題 暴力法 暴力法稍加改進(然並卵) 雙指標法(推薦) 二分查詢法(不懂) Markdown版本筆記 我的GitHub首頁

領扣-209 長度的子數組 Minimum Size Subarray Sum MD

二分查找 -m 子數組 雙指針法 amp 滿足 android tco 成了 目錄 長度最小的子數組 Minimum Size Subarray Sum -209 問題 暴力法 暴力法稍加改進(然並卵) 雙指針法(推薦) 二分查找法(不懂) Markdown版本

[Swift]LeetCode209. 長度的子陣列 | Minimum Size Subarray Sum

Given an array of n positive integers and a positive integer s, find the minimal length of a contiguous subarray of which the sum

LeetCode 209. 長度的子數組(Minimum Size Subarray Sum

leet 不存在 vector amp 記錄 移動 連續子數組 num mil 題目描述 給定一個含有 n 個正整數的數組和一個正整數 s ,找出該數組中滿足其和 ≥ s 的長度最小的連續子數組。如果不存在符合條件的連續子數組,返回 0。 示例: 輸入:

**209. Minimum Size Subarray Sum 長度的子數組

連續子數組 mini 存在 ins else 越界 雙指針 ray nim 1. 題目描述 給定一個含有 n 個正整數的數組和一個正整數 s ,找出該數組中滿足其和 ≥ s 的長度最小的連續子數組。如果不存在符合條件的連續子數組,返回 0。 示例:

【LeetCode】短子陣列之和(Minimum size subarray sum

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

leetcode 209. Minimum Size Subarray Sum

urn https scrip subarray () ins -s mar color https://leetcode.com/problems/minimum-size-subarray-sum/#/description 題目很簡單。題意就是求字符串中長度最小的一

209. Minimum Size Subarray Sum

ren cto mos n) gin sum targe osi minimal Problem statement: Given an array of n positive integers and a positive integer s, find the min

Leetcode 209: Minimum Size Subarray Sum

urn bar div while public ima ger pub ini Given an array of n positive integers and a positive integer s, find the minimal length of a con

Leetcode 931. Minimum falling path sum 下降路徑(動態規劃)

Leetcode 931. Minimum falling path sum 最小下降路徑和(動態規劃) 題目描述 已知一個正方形二維陣列A,我們想找到一條最小下降路徑的和 所謂下降路徑是指,從一行到下一行,只能選擇間距不超過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 contiguous subarray of which the sum ≥ s. If the

#Leetcode# 209. Minimum Size Subarray Sum

problems href pan class input subarray tar empty lex https://leetcode.com/problems/minimum-size-subarray-sum/ Given an array of n posit

LeetCode: 209. Minimum Size Subarray Sum

題目描述 Given an array of ``n positive integers and a positive integer s, find the minimal length of a

【LeetCode】209. Minimum Size Subarray Sum 解題報告(Python)

題目描述: Given an array of n positive integers and a positive integer s, find the minimal length of a

【LeetCode】Minimum Size Subarray Sum

採用類似滑動視窗的形式,雙指標i,j,複雜度O(n). 與https://leetcode.com/problems/minimum-window-substring/  這題有點類似 public class Solution { public int minS

第五週演算法分析與設計:Minimum Size Subarray Sum

演算法描述: Given an array of n positive integers and a positive integer s, find the minimal length of