描述:

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.

思路一:动态规划

因为这是个优化问题。并且问题可以分解为一个个子问题,所以利用DP来解决此问题是一种很好的解决方案:

dp[i-1]到dp[i]的转换分两种情况:

        1)dp[i-1] > 0: 当大于0时,dp[i] = dp[i-1] + num[i]

        2) dp[i-1] < 0:当大于0时,dp[i] = 0  (因为此时将剔除dp[i-1]的影响)

class Solution {
public:
int maxSubArray(vector<int>& nums) {
vector<int> dp(nums.size());
dp[] = nums[];
int max_ans = dp[];
for(int i = ;i<nums.size();++i){
dp[i] = nums[i] + (dp[i-] > ? dp[i-] : );
max_ans = max(max_ans, dp[i]);
}
return max_ans;
}
};

思路二:贪婪

从左到右汇总数组时找到总和最优解,和dp类似,但是我们这里不保存dp的状态,只记录临时sum和最大sum

class Solution {
public:
int maxSubArray(vector<int>& nums) {
int sum = ;
int ans;
ans = nums[];
for(int i = ;i<nums.size();i++){
sum+=nums[i];
ans = max(ans,sum);
sum = max(sum,);
}
return ans;
}
};

思路三:分治法

分治法的思路是将问题不断二分,分到不能再分,然后再将计算完的数据整合归一,最后得出最优解,这里,如图所示,将数组不断二分,然后取出每一段的最大sum,然后传回总函数,然后输出最优解

class Solution {
public:
int maxSubArray(vector<int>& nums) {
if(nums.size()==) return ;
return maxSubArray(nums, , nums.size() - );
} // l代表数组左端low,h代表数组右端high,返回最大sum
int maxSubArray(vector<int>& arr, int l, int h)
{
// Base Case: Only one element
if (l == h)
return arr[l]; // Find middle point
int m = (l + h)/; /* Return maximum of following three possible cases
a) Maximum subarray sum in left half
b) Maximum subarray sum in right half
c) Maximum subarray sum such that the subarray crosses the midpoint */
return max(max(maxSubArray(arr, l, m), //最左侧数组求最大sum
maxSubArray(arr, m+, h)), //对右侧数组求最大sum ,之后求左右的最大值
maxCrossingSum(arr, l, m, h)); //对整个数组以mid为分界线求最大sum
} int maxCrossingSum(vector<int>& arr, int l, int m, int h)
{
// Include elements on left of mid.
int sum = ;
int left_sum = INT_MIN;
for (int i = m; i >= l; i--)
{
sum = sum + arr[i];
if (sum > left_sum)
left_sum = sum;
} // Include elements on right of mid
sum = ;
int right_sum = INT_MIN;
for (int i = m+; i <= h; i++)
{
sum = sum + arr[i];
if (sum > right_sum)
right_sum = sum;
} // Return sum of elements on left and right of mid
return left_sum + right_sum;
}
};

【LeetCode】最大子阵列 Maximum Subarray(贪婪&分治)的更多相关文章

  1. 小旭讲解 LeetCode 53. Maximum Subarray 动态规划 分治策略

    原题 Given an integer array nums, find the contiguous subarray (containing at least one number) which ...

  2. LeetCode Array Easy 53. Maximum Subarray 个人解法 和分治思想的学习

    Description Given an integer array nums, find the contiguous subarray (containing at least one numbe ...

  3. LeetCode练题——53. Maximum Subarray

    1.题目 53. Maximum Subarray——Easy Given an integer array nums, find the contiguous subarray (containin ...

  4. LeetCode OJ平台上Maximum Subarray题目O(n)复杂度解决方式

    原始题目例如以下,意为寻找数组和最大的子串,返回这个最大和就可以. Find the contiguous subarray within an array (containing at least ...

  5. LeetCode之“动态规划”:Maximum Subarray

    题目链接 题目要求: Find the contiguous subarray within an array (containing at least one number) which has t ...

  6. [LeetCode&Python] Problem 53. Maximum Subarray

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

  7. [LeetCode]题53:Maximum Subarray

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

  8. [leetcode.com]算法题目 - Maximum Subarray

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

  9. LeetCode(53) Maximum Subarray

    题目 Find the contiguous subarray within an array (containing at least one number) which has the large ...

随机推荐

  1. iOS开发UITableViewCell的选中时的颜色设置(转)

    iOS开发UITableViewCell的选中时的颜色设置   1.系统默认的颜色设置 //无色 cell.selectionStyle = UITableViewCellSelectionStyle ...

  2. 基于AR9331(MIPS架构)分析系统启动过程(uboot)

    前提: 1.AR9331是基于MIPS 24K CPU的一款WIFI1X1芯片,其SDK采用uboot作为引导.AR9331中定义的基地址是:0x9f00,0000 2.MIPS24K芯片,将固定的起 ...

  3. abp的权限与导航菜单的关系

    原来以为各是各的,所以就有了第一个版本.Getallmentus.然后注入了role,当然失败了.获取所有的菜单.一直在思考在什么地方设置菜单是否展示呢? 后面看了源码.才发现自己错了. UserNa ...

  4. GCD XOR UVA 12716 找规律 给定一个n,找多少对(a,b)满足1<=b<=a<=n,gcd(a,b)=a^b;

    /** 题目:GCD XOR UVA 12716 链接:https://vjudge.net/problem/UVA-12716 题意:给定一个n,找多少对(a,b)满足1<=b<=a&l ...

  5. Swoole系列(二):安装

    Window是没办法安装的,服务器版本建议用linux的centos7 Php版本5.4 安装步骤: 1.更新你的yum yum update 2.安装php相关扩展 2.yum install ph ...

  6. 使用加密的squid配合stunnel实现HTTP代理

    现在大部分人都是用ssh tunnel来搭建socks5代理,其实这种方式效率并不高,ssh tunnel并不是为了做代理而存在的.一个比较好的方法是加密squid配合stunnel实现http代理. ...

  7. sql limit offset 区别

    select * from table limit 2,1;                  //含义是跳过2条取1条数据,即读取第3条数据 select * from table limit 2 ...

  8. 面向对象JSON的继承(复制)与函数的继承(复制)

    今天这里和大家分享下如何复制对象 的属性 创建 对象的方式有三种,这里和大家分享下最常用的几种 1.JSON格式的方式创建对象 2.用函数的方式创建,然后用new关键字实例化对象,关于this的指向问 ...

  9. java 获取网页指定内容

    import java.io.BufferedReader; import java.io.InputStreamReader; import java.net.HttpURLConnection; ...

  10. [浪风分享]App必死 Web永生 看Web的前世今生 必会卷土重来

    当我们回顾技术的演变历史时,我们也应该关注技术演变的背后逻辑. 几年前,美国的<连线>杂志发表了“Web已死,Internet永生”的文章,由于作者之一是长尾理论的提出者克里斯.安德森(C ...