zoukankan      html  css  js  c++  java
  • Minimum Subarray

    Given an array of integers, find the subarray with smallest sum.

    Return the sum of the subarray.

     Notice

    The subarray should contain one integer at least.

    For [1, -1, -2, 1], return -3.

    Analyse: Greedy. If the previous sum is larger than 0, then we shouldn't combine the current value with the previous sum. Otherwise, we should combine them together. Every time we deal with a value, we should update the minSum. 

    Runtime: 36ms

     1 class Solution {
     2 public:
     3     /**
     4      * @param nums: a list of integers
     5      * @return: A integer denote the sum of minimum subarray
     6      */
     7     int minSubArray(vector<int> nums) {
     8         // write your code here
     9         
    10         // greedy
    11         if (nums.empty()) return 0;
    12         
    13         int minSum = INT_MAX, tempSum = 0;
    14         for (int i = 0; i < nums.size(); i++) {
    15             if (tempSum > 0) 
    16                 tempSum = nums[i];
    17             else 
    18                 tempSum += nums[i];
    19                 
    20             minSum = min(minSum, tempSum);
    21         }
    22         return minSum;
    23     }
    24 };
  • 相关阅读:
    接口测试
    Appium应用
    adb常用指令与APPium环境搭建
    移动端专项测试
    tomcat修改端口号
    Linux之Redis安装
    FTL常用标签及语法
    .ftl文件介绍
    maven之clean、install命令
    tomcat环境变量详细配置步骤
  • 原文地址:https://www.cnblogs.com/amazingzoe/p/5864384.html
Copyright © 2011-2022 走看看