zoukankan      html  css  js  c++  java
  • 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 -1 instead.

    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.

    Challenge 

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

    Analyse: Two pointers. O(n)

    Runtime: 203ms

     1 class Solution {
     2 public:
     3     /**
     4      * @param nums: a vector of integers
     5      * @param s: an integer
     6      * @return: an integer representing the minimum size of subarray
     7      */
     8     int minimumSize(vector<int> &nums, int s) {
     9         // write your code here
    10         // mark left, set right as left + 1
    11         // move right until sum of nums[left...right] >= s
    12         // update the min length and let left move one position
    13         // if current sum >= s update min length and keep move left
    14         // if current sum < s move right one position
    15         
    16         if (nums.empty()) return -1;
    17         int left = 0, right = 1;
    18         int minSize = INT_MAX, tempSum = nums[left];
    19         if (tempSum > s) return 1;
    20         while (left < nums.size() && right < nums.size()) {
    21             tempSum += nums[right];
    22             while (tempSum >= s) {
    23                 minSize = min(minSize, right - left + 1);
    24                 tempSum -= nums[left];
    25                 left++;
    26             }
    27             if (tempSum < s) {
    28                 right++;
    29             }
    30         }
    31         return minSize == INT_MAX ? -1 : minSize;
    32     }
    33 };

    Analyse: Brute force. O(n^2)

    Runtime: 605ms

     1 class Solution {
     2 public:
     3     /**
     4      * @param nums: a vector of integers
     5      * @param s: an integer
     6      * @return: an integer representing the minimum size of subarray
     7      */
     8     int minimumSize(vector<int> &nums, int s) {
     9         // write your code here
    10         if (nums.empty()) return -1;
    11         
    12         int result = INT_MAX;
    13         for (int i = 0; i < nums.size(); i++) {
    14             int tempSum = nums[i];
    15             if (tempSum >= s) return 1;
    16             for (int j = i + 1; j < nums.size(); j++) {
    17                 tempSum += nums[j];
    18                 if (tempSum >= s) {
    19                     result = min(result, j - i + 1);
    20                     break;
    21                 }
    22             }
    23         }
    24         return result == INT_MAX ? -1 : result;
    25     }
    26 };
    View Code
  • 相关阅读:
    springboot使用war包部署到外部tomcat
    html只允许输入的数据校验,只允许输入字母汉字数字等
    转:执行ajax加载页面中的js
    php 解析xml 的四种方法(转)
    Php 获取xml中的节点值
    php中DOMDocument简单用法(XML创建、添加、删除、修改)
    PHP对XML文件操作详细
    转载 PHP 程序员学数据结构与算法之《栈》
    php读取二进制流(C语言结构体struct数据文件)的深入解析
    关于php和C语言接口的结构传递问题,udp,tcp通信
  • 原文地址:https://www.cnblogs.com/amazingzoe/p/5820400.html
Copyright © 2011-2022 走看看