原题

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.

讲解

动态规划

视频@ 哔哩哔哩 动态规划 or YouTube 动态规划

通过把原问题分解为相对简单的子问题的方式求解复杂问题的方法。

动态规划的性质

  1. 最优子结构(optimal sub-structure):如果问题的最优解所包含的子问题的解也是最优的,我们就称该问题具有最优子结构性质(即满足最优化原理)。最优子结构性质为动态规划算法解决问题提供了重要线索。
  2. 重叠子问题(overlapping sub-problem):动态规划算法正是利用了这种子问题的重叠性质,对每一个子问题只计算一次,然后将其计算结果保存在一个表格中,当再次需要计算已经计算过的子问题时,只是在表格中简单地查看一下结果,从而获得较高的效率。

状态转移方程

dp[i] = max(nums[i], nums[i] + dp[i - 1])

解释

  • i代表数组中的第i个元素的位置
  • dp[i]代表从0到i闭区间内,所有包含第i个元素的连续子数组中,总和最大的值

nums = [-2,1,-3,4,-1,2,1,-5,4]

dp = [-2, 1, -2, 4, 3, 5, 6, 1, 5]

时间复杂度

O(n)

参考

代码(C++)

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

分治策略

视频@ 哔哩哔哩 分治策略 or YouTube 分治策略

 

分治算法是一个解决复杂问题的好工具,它可以把问题分解成若干个子问题,把子问题逐个解决,再组合到一起形成大问题的答案。

这个技巧是很多高效算法的基础,如排序算法快速排序归并排序

实现方式

循环递归

在每一层递归上都有三个步骤:

  1. 分解:将原问题分解为若干个规模较小,相对独立,与原问题形式相同的子问题。
  2. 解决:若子问题规模较小且易于解决 时,则直接解。否则,递归地解决各子问题。
  3. 合并:将各子问题的解合并为原问题的解。

注意事项

  • 边界条件,即求解问题的最小规模的判定

示意图

递归关系式

T(n) = 2T(n/2) + n

可以利用递归树的方式求解其时间复杂度(其求解过程在《算法导论》中文第三版 P51有讲解)

时间复杂度

O(nlgn)

代码(C++)

class Solution {
public:
int maxSubArray(vector<int>& nums) {
return find(nums, 0, nums.size() - 1);
} int find(vector<int>& nums, int start, int end) {
// boundary
if (start == end) {
return nums[start];
}
if (start > end) {
return INT_MIN;
} // delcare
int left_max = 0, right_max = 0, ml = 0, mr = 0;
int middle = (start + end) / 2; // find
left_max = find(nums, start, middle - 1);
right_max = find(nums, middle + 1, end);
// middle
// to left
for (int i = middle - 1, sum = 0; i >= start; --i) {
sum += nums[i];
if (ml < sum) ml = sum;
}
// to right
for (int i = middle + 1, sum = 0; i <= end; ++i) {
sum += nums[i];
if (mr < sum) mr = sum;
} // return
return max(max(left_max, right_max), ml + mr + nums[middle]);
}
};


原题:https://leetcode.com/problems/maximum-subarray

文章来源:胡小旭 => 小旭讲解 LeetCode 53. Maximum Subarray

小旭讲解 LeetCode 53. Maximum Subarray 动态规划 分治策略的更多相关文章

  1. [array] leetcode - 53. Maximum Subarray - Easy

    leetcode - 53. Maximum Subarray - Easy descrition Find the contiguous subarray within an array (cont ...

  2. Leetcode#53.Maximum Subarray(最大子序和)

    题目描述 给定一个序列(至少含有 1 个数),从该序列中寻找一个连续的子序列,使得子序列的和最大. 例如,给定序列 [-2,1,-3,4,-1,2,1,-5,4], 连续子序列 [4,-1,2,1] ...

  3. LN : leetcode 53 Maximum Subarray

    lc 53 Maximum Subarray 53 Maximum Subarray Find the contiguous subarray within an array (containing ...

  4. leetcode 53. Maximum Subarray 、152. Maximum Product Subarray

    53. Maximum Subarray 之前的值小于0就不加了.dp[i]表示以i结尾当前的最大和,所以需要用一个变量保存最大值. 动态规划的方法: class Solution { public: ...

  5. 41. leetcode 53. Maximum Subarray

    53. Maximum Subarray Find the contiguous subarray within an array (containing at least one number) w ...

  6. LeetCode 53. Maximum Subarray(最大的子数组)

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

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

    Maximum Subarray  Find the contiguous subarray within an array (containing at least one number) whic ...

  8. [LeetCode] 53. Maximum Subarray 最大子数组 --动态规划+分治

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

  9. [LeetCode] 53. Maximum Subarray 最大子数组

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

随机推荐

  1. 【luogu P2580 于是他错误的点名开始了】 题解

    题目链接:https://www.luogu.org/problemnew/show/P2580 我真的永远都爱stl #include <map> #include <cstdio ...

  2. 关于ProjectServer定制化项目中心页面

    ProjectServer界面很多客户接受不了,随便用户可以根据自己需要展示页面,但大多数国内用户喜欢确定的样式,我就是要这样的页面,不要个人定制. 那只有自己再做一个项目中心的webpart嵌入,对 ...

  3. Linux7静默安装Oracle11g教程,亲测实用有效!

    1.查看swap大小,若小于150M,需添加增加虚拟空间 dd if=/dev/zero of=/swapadd bs=1024 count=2006424 mkswap /swapadd swapo ...

  4. 替换html里面的\r\n及解决记事本中的每个段落只有一行的情形

    1. 在用python爬取小说的时候, 发现在内容里每次换行都有\r\n(即回车, 换行)出现. 此时可以采用  s.replace('\\r\\n','') , 其中s为字符串类型. 2. 在爬取完 ...

  5. 关于Navicat连接MySQL 报 Authentication plugin 'caching_sha2_password' cannot be loaded

    报错原因: 报这个错是因为MySQL8使用了 caching_sha2_password 加密方式而之前MySQL使用的是 mysql_native_password 加密方式,而你的Navicat不 ...

  6. shiro笔记-"Authentication failed for token submission [org.apache.shiro.authc.UsernamePasswordToken - ylw, rememberMe=false]. Possible unexpected error? (Typical or expected login exceptions should ext

    在学习shiro过程中遇到这个错误,在网上找了好久资料也没找到解决办法,大概都是说和传入的值有问题.于是我试着耐心看我自己的报错信息,最终找到了原因并解决.每个人的问题可能都会有差异,所以建议大家耐心 ...

  7. 强化记忆之php

    php 输出的区分 新手摸索道路,有说不对的地方,还请多多包涵. echo 能够输出一个以上的字符串,也能输出html标签 print  一次只能接受一个字符串(区分与echo),也能输出html标签 ...

  8. 3.2 进程间通信之fifo

    一.引言 FIFO常被称为有名管道,不同于管道(pipe).pipe仅适用于“有血缘关系”的IPC.但FIFO还可以应用于不相关的进程的IPC.实际上,FIFO是Linux基础文件类型中的一种,是在读 ...

  9. Python学习第二弹

    昨天补充: 编码: Unicode ; utf-8 ; GBK       关系:   关键字:1. continue 终止当前循环,进行下一次循环 2. break      终止循环 题6解法2: ...

  10. python装饰器+递归+冒泡排序

    冒泡排序 li = [33, 2, 10, 1,23,23523,5123,4123,1,2,0] for k in range(1,len(li)): for i in range(len(li) ...