19.2.8 [LeetCode 53] Maximum Subarray

Given an integer array nums, find the contiguous subarray (containing at least one number) which has the largest sum and return its sum.

Example:

Input: [-2,1,-3,4,-1,2,1,-5,4],
Output: 6
Explanation: [4,-1,2,1] has the largest sum = 6.

Follow up:

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)解法

 1 class Solution {
 2 public:
 3     int maxSubArray(vector<int>& nums) {
 4         int e = 1, n = nums.size(), ans = nums[0], now = nums[0];
 5         while (e < n) {
 6             now = max(now + nums[e], nums[e]);
 7             ans = max(now, ans);
 8             e++;
 9         }
10         return ans;
11     }
12 };

然后是分治算法

 1 class Solution {
 2 public:
 3     int maxSubRange(vector<int>&nums, int x, int y) {
 4         if (x == y)return nums[x];
 5         int mid = (x + y) / 2;
 6         int lmax = maxSubRange(nums, x, mid), rmax = maxSubRange(nums, mid + 1, y);
 7         int tmp = 0, mmax = 0;
 8         for (int i = mid - 1; i >= x; i--) {
 9             tmp += nums[i];
10             mmax = max(tmp, mmax);
11         }
12         tmp = mmax;
13         for (int i = mid; i <= y; i++) {
14             tmp += nums[i];
15             mmax = max(tmp, mmax);
16         }
17         return max(mmax, max(lmax, rmax));
18     }
19     int maxSubArray(vector<int>& nums) {
20         int e = 1, n = nums.size(), ans = nums[0], now = nums[0];
21         while (e < n) {
22             now = max(now + nums[e], nums[e]);
23             ans = max(now, ans);
24             e++;
25         }
26         return ans;
27     }
28 };

原文地址:https://www.cnblogs.com/yalphait/p/10356399.html

时间: 2024-10-05 08:48:24

19.2.8 [LeetCode 53] Maximum Subarray的相关文章

41. leetcode 53. Maximum Subarray

53. 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. 思路:这个题还挺经典

LeetCode 53. 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. click to show more practice. Mor

leetCode 53.Maximum Subarray (子数组的最大和) 解题思路方法

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. click to show

[LeetCode] 53. 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. 问题: 给定一个元素有正有负的数组,求最大连续子数组的和. 思路

leetcode 53 Maximum Subarray ----- java

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. 这道题就是求最大子串和 肯定是o(n)的时间和o(1)的空间 就

Leetcode 53 Maximum Subarray

public class S053 { //kadane's algorithm //最大子数组第一个数肯定大于零,从最大子数组中某个数开始往前的数之和肯定大于零 public int maxSubArray(int[] nums) { if(nums.length == 0) return 0; int sum = 0; int result = Integer.MIN_VALUE; for(int i =0;i<nums.length;i++){ if(sum<0) sum=0; sum+

LeetCode 53. Maximum Subarray 最大连续字段和问题

考察:最大连续字段和问题. 解决问题时间复杂度:O(n) 问题隐含条件:如果给出的数集都是负数,那么最大连续字段和就是,最大的那个负数. eg:{-2,-1}  结果应该输出 -1 而不是 0 int maxSubArray(int* nums, int numsSize) { int maxSum = 0; //维护最大连续字段和 int currentMaxSum = 0;//当前最大和 int nextNum = 0; int singleSum = nums[0]; //存在全是负数,则

LeetCode练题——53. Maximum Subarray

1.题目 53. Maximum Subarray——Easy Given an integer array nums, find the contiguous subarray (containing at least one number) which has the largest sum and return its sum. Example: Input: [-2,1,-3,4,-1,2,1,-5,4], Output: 6 Explanation: [4,-1,2,1] has th

Leetcode 动态规划 Maximum Subarray

正整数或一位小数或者俩位小数的正则表达式的写法 ^(?!0+(?:\.0+)?$)(?:[1-9]\d*|0)(?:\.\d{1,2})?$ Leetcode 动态规划 Maximum Subarray,布布扣,bubuko.com