[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.
这道题让求数组中和最大的子数组的和,类似这种“问题的子问题的最优解”问题首先想到的是使用动态规划的方法求解,使用动态规划的难点在于使用适当的dp数组写出问题的正确的状态转移方程,本题的状态转移方程可以表示为:dp[i]=max(dp[i-1]+nums[i],nums[i]),其中dp[i]表示数组中以nums[i]结尾的和最大的子数组的和。这个解法的时间复杂度是 O(n),内存消耗O(n)。代码如下maxSubArray_dp_normal函数所示。由于在计算dp[i]的时候只需要用到dp[i-1],所有可以用一个变量代替dp数组,进行了空间压缩,实现时间复杂度是 O(n),内存消耗O(1),是本题的最优解法,代码如maxSubArray_dp_selected函数所示。
动态规划:
//动态规划 时间O(n) 空间O(n)
class Solution {
public:
int maxSubArray(vector<int>& nums) {
//return maxSubArray_dp_normal(nums);
return maxSubArray_dp_seleted(nums);
}
int maxSubArray_dp_normal(vector<int>& nums)
{
const int nums_size = nums.size();
vector<int> dp = nums;//dp[i]表示以nums[i]结尾的和最大的子序列的和
int res = dp[0];
for(int i = 1;i<nums_size;++i)
{
dp[i] = max(dp[i-1]+nums[i],nums[i]);//状态转移方程
res = max(res,dp[i]);
}
return res;
}
//空间优化了的dp,时间O(n) 空间O(1)
int maxSubArray_dp_seleted(vector<int> &nums)
{
const int nums_size = nums.size();
int dp = nums[0];
int res = dp;
for(int i = 1;i<nums_size;++i)
{
dp = max(dp+nums[i],nums[i]);//状态转移方程
res = max(res,dp);
}
return res;
}
};
题目还要求我们用分治法 Divide and Conquer Approach 来解,这个分治法的思想就类似于二分搜索法,需要把数组一分为二,分别找出左边和右边的最大子数组之和,然后还要考虑最大和子数组同时跨越左右两个子数组的情况,具体是从中间开始向左右分别扫描,求出的最大值分别和左右两边得出的最大值相比较取三者最大的那一个,时间复杂度是O(nlogn),显然此种方法不是本题的最优解,分治法代码如下:
分治:
[LeetCode] 53. Maximum Subarray 最大子数组 --动态规划+分治的更多相关文章
- [LeetCode] 53. Maximum Subarray 最大子数组
Given an integer array nums, find the contiguous subarray (containing at least one number) which has ...
- [leetcode]53. Maximum Subarray最大子数组和
Given an integer array nums, find the contiguous subarray (containing at least one number) which has ...
- LeetCode 53. Maximum Subarray最大子序和 (C++)
题目: Given an integer array nums, find the contiguous subarray (containing at least one number) which ...
- 小旭讲解 LeetCode 53. Maximum Subarray 动态规划 分治策略
原题 Given an integer array nums, find the contiguous subarray (containing at least one number) which ...
- [array] leetcode - 53. Maximum Subarray - Easy
leetcode - 53. Maximum Subarray - Easy descrition Find the contiguous subarray within an array (cont ...
- Leetcode#53.Maximum Subarray(最大子序和)
题目描述 给定一个序列(至少含有 1 个数),从该序列中寻找一个连续的子序列,使得子序列的和最大. 例如,给定序列 [-2,1,-3,4,-1,2,1,-5,4], 连续子序列 [4,-1,2,1] ...
- leetcode 53. Maximum Subarray 、152. Maximum Product Subarray
53. Maximum Subarray 之前的值小于0就不加了.dp[i]表示以i结尾当前的最大和,所以需要用一个变量保存最大值. 动态规划的方法: class Solution { public: ...
- 41. leetcode 53. Maximum Subarray
53. Maximum Subarray Find the contiguous subarray within an array (containing at least one number) w ...
- LN : leetcode 53 Maximum Subarray
lc 53 Maximum Subarray 53 Maximum Subarray Find the contiguous subarray within an array (containing ...
随机推荐
- ASP 数据库分页
<%@LANGUAGE="VBSCRIPT" CODEPAGE="936"%> <% Response.buffer=false %> ...
- python 卡方检验例子
python 求拒绝域和卡方值 import scipy.stats as ss obs=[107,198,192,125,132,248] exp=[167]*6 #拒绝域 1%的显著水平,自由度5 ...
- 使用@Value注解对bean进行属性注入
使用@Value注解,可以有三种属性注入的方式: 1. 使用字面量注入 2. 使用EL表达式注入 3. 使用占位符注入 import org.springframework.beans.factory ...
- Delphi XE2 之 FireMonkey 入门(19) - TFmxObject 的子类们(表)
参考: 和 FMX 相关的类(表) TFmxObject IFreeNotification TAnimation TBitmapAnimation TBi ...
- 【ABAP系列】SAP ABAP 利用class创建客户/供应商主数据
公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[MM系列]SAP ABAP 利用class创建 ...
- 关于ES6语法的 一些新的特性
1.新的变量声明 :let :块级作用域,解决全局污染问题 const :常量 ,如π:3.1415927 class :类 .var:弱类型 funciton :方法 , import : 导入参 ...
- 第四周总结&第二次实验报告
实验二 Java简单类与对象 实验目的 掌握类的定义,熟悉属性.构造函数.方法的作用,掌握用类作为类型声明变量和方法返回值: 理解类和对象的区别,掌握构造函数的使用,熟悉通过对象名引用实例的方法和属性 ...
- centos7下搭建Testlink环境详细过程
花了半天的时间终于搭建好了完整的Testlink环境,主要包括Mysql以及PHP的版本.未关闭防火墙.以及安装配置过程中遇到的一些问题.以下是详细的搭建过程. 一.工具准备 以下是我在搭建过程中用到 ...
- kafka+hbase+hive实现实时接入数据至hive
整体架构: 项目目标,实现配置mysql,便可以自动化入湖至Hive,入湖至Hive方便后期数据分析. 首先在Mysql中配置好kafka的topic.Server以及入户表等信息,java程序初始化 ...
- POJ-3468 A Simple Problem with Integers (区间求和,成段加减)
You have N integers, A1, A2, ... , AN. You need to deal with two kinds of operations. One type of op ...