LeetCode:Maximum Subarray

简介:

Find the contiguous subarray within an array (containing at least one number) which has the largest sum.

For example, given the array [−2,1,−3,4,−1,2,1,−5,4]
the contiguous subarray [4,−1,2,1] has the largest sum = 6.

More practice:

If you have figured out the O(n) solution, try coding another solution using the divide and conquer approach, which is more subtle.


可以参考我的另一篇博文 最大子数组和(最大子段和)

下面分别给出O(n)的动态规划解法和O(nlogn)的分治解法                              本文地址

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
class  Solution {
public :
     int  maxSubArray( int  A[], int  n) {
         //最大字段和问题
         int  res = INT_MIN, sum = -1;
         for ( int  i = 0; i < n; i++)
         {
             if (sum > 0)
                 sum += A[i];
             else  sum = A[i];
             if (sum > res)res = sum;
         }
         return  res;
     }
};

 

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
class  Solution {
public :
     int  maxSubArray( int  A[], int  n) {
         //最大字段和问题
         return  helper(A, 0, n-1);
     }
private :
     int  helper( int  A[], const  int  istart, const  int  iend)
     {
         if (istart == iend) return  A[iend];
         int  middle = (istart + iend) / 2;
         int  maxLeft = helper(A, istart, middle);
         int  maxRight = helper(A, middle + 1, iend);
         int  midLeft = A[middle];
         int  tmp = midLeft;
         for ( int  i = middle - 1; i >= istart; i--)
         {
             tmp += A[i];
             if (midLeft < tmp)midLeft = tmp;
         }
         int  midRight = A[middle + 1];
         tmp = midRight;
         for ( int  i = middle + 2; i <= iend; i++)
         {
             tmp += A[i];
             if (midRight < tmp)midRight = tmp;
         }
         return  max(max(maxLeft, maxRight), midLeft + midRight);
     }
};





本文转自tenos博客园博客,原文链接:http://www.cnblogs.com/TenosDoIt/p/3713525.html,如需转载请自行联系原作者

目录
相关文章
|
7月前
Leetcode 53.Maximum Subarray
题意简单,给出一个数组,求出其中最大的子数组和。 这种简单题目背后蕴藏着很巧妙的解题方法。其实只需要遍历一次数组就可以求得解。 思路是这样的,你想想看,如果一段子数组的和是负数, 那么这一段子数组不可能是最大和数组的一部分,丢掉重新从下一个位置开始选。
32 0
|
算法
LeetCode 414. Third Maximum Number
给定一个非空数组,返回此数组中第三大的数。如果不存在,则返回数组中最大的数。要求算法时间复杂度必须是O(n)。
72 0
LeetCode 414. Third Maximum Number
LeetCode 318. Maximum Product of Word Lengths
给定一个字符串数组 words,找到 length(word[i]) * length(word[j]) 的最大值,并且这两个单词不含有公共字母。你可以认为每个单词只包含小写字母。如果不存在这样的两个单词,返回 0。
61 0
LeetCode 318. Maximum Product of Word Lengths
LeetCode 239. Sliding Window Maximum
给定一个数组 nums,有一个大小为 k 的滑动窗口从数组的最左侧移动到数组的最右侧。你只可以看到在滑动窗口 k 内的数字。滑动窗口每次只向右移动一位。 返回滑动窗口最大值。
45 0
LeetCode 239. Sliding Window Maximum
LeetCode 209. Minimum Size Subarray Sum
给定一个含有 n 个正整数的数组和一个正整数 s ,找出该数组中满足其和 ≥ s 的长度最小的连续子数组。如果不存在符合条件的连续子数组,返回 0。
93 0
LeetCode 209. Minimum Size Subarray Sum
LeetCode 164. Maximum Gap
给定一个无序的数组,找出数组在排序之后,相邻元素之间最大的差值。 如果数组元素个数小于 2,则返回 0。
61 0
LeetCode 164. Maximum Gap
LeetCode 152. Maximum Product Subarray
给定一个整数数组 nums ,找出一个序列中乘积最大的连续子序列(该序列至少包含一个数)。
35 0
LeetCode 152. Maximum Product Subarray
LeetCode contest 190 5417. 定长子串中元音的最大数目 Maximum Number of Vowels in a Substring of Given Length
LeetCode contest 190 5417. 定长子串中元音的最大数目 Maximum Number of Vowels in a Substring of Given Length
LeetCode Contest 186 5392. 分割字符串的最大得分 Maximum Score After Splitting a String
LeetCode Contest 186 5392. 分割字符串的最大得分 Maximum Score After Splitting a String
LeetCode 104. 二叉树的最大深度 Maximum Depth of Binary Tree
LeetCode 104. 二叉树的最大深度 Maximum Depth of Binary Tree