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.

Example

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 };
时间: 2024-10-10 16:43:00

Minimum Subarray的相关文章

leetcode:Minimum Subarray

1. Given an array of integers, find the subarray with smallest sum. Return the sum of the subarray. For [1, -1, -2, 1], return -3 2. 1.只需要求出最小值 2.利用sum,来判断最小值 3.比较最小值 3.代码: public static int minSubArray(ArrayList<Integer> nums) { if(nums == null ||

LintCode-Minimum Subarray

Given an array of integers, find the subarray with smallest sum. Return the sum of the subarray. Note The subarray should contain at least one integer. Example For [1, -1, -2, 1], return -3 Solution: 1 public class Solution { 2 /** 3 * @param nums: a

lintcode.44 最小子数组

最小子数组 描述 笔记 数据 评测 给定一个整数数组,找到一个具有最小和的子数组.返回其最小和. 注意事项 子数组最少包含一个数字 您在真实的面试中是否遇到过这个题? Yes 哪家公司问你的这个题? Airbnb Amazon LinkedIn Cryptic Studios Dropbox Apple Epic Systems TinyCo Yelp Hedvig Zenefits Uber Snapchat Yahoo Microsoft Bloomberg Facebook Google

LintCode Problems Link Table

Practice Makes Perfect! # Problem Link Tag Difficulty 1 A + B problem Bitwise Operation Easy 2 Trailing Zeros Math Easy 3 Digit Counts   Medium 4 Ugly Number II   Medium 5 Kth Largest Element   Medium 6 Merge Two Sorted Arrays   Easy 7 Binary Tree Se

LintCode数组题总结

做算法题的时候,几乎不可避免要跟数组打交道.在LintCode上数组那一章有这么一些题目: 1)547. Intersection of Two Arrays 比较简单.要求找到2个数组的交集,简单点的方法就是用2个hashSet,第一个HashSet存第一个数组的元素.然后扫描第二个数组,如果第二个数组中的元素在第一个HashSet中出现了,那么就把它加到第二个HashSet中.最后第二个HashSet就是两个数组的交集了. 2)138. Subarray Sum 要求在一个数组中找到一个子数

在线处理算法

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 if (nums.size() == 1) { 10 return nums[0]; 11 } e

LintCode Python 简单级题目 最小子数组和、最大子数组和

题目1 最小子数组 描述: 给定一个整数数组,找到一个具有最小和的子数组.返回其最小和. 注意事项 子数组最少包含一个数字 您在真实的面试中是否遇到过这个题? Yes 样例 给出数组[1, -1, -2, 1],返回 -3 标签 LintCode 版权所有 子数组 贪心 数组 题目2 最大子数组 描述: 给定一个整数数组,找到一个具有最大和的子数组,返回其最大和. 注意事项 子数组最少包含一个数 您在真实的面试中是否遇到过这个题? Yes 样例 给出数组[?2,2,?3,4,?1,2,1,?5,

[容易]最小子数组

题目来源:http://www.lintcode.com/zh-cn/problem/minimum-subarray/ 解法同最大子数组: 令currSum为当前最小子数组的和,minSum为最后要返回的最小子数组的和.a[j]有两种选择,要么放入前面的子数组,要么作为新子数组的第一个元素.如果currSum加上当前元素a[j]后不大于a[j],则令currSum再加上a[j].否则currSum重新赋值为a[j].同时,当currSum < minSum,则更新minSum = currSu

最小子数组

class Solution {public: /* * @param nums: a list of integers * @return: A integer indicate the sum of minimum subarray */ int minSubArray(vector<int> &nums) { // write your code here int sum = 0, flag = 0; for (int i = 0; i < nums.size(); i++