作者: 负雪明烛
id: fuxuemingzhu
个人博客: http://fuxuemingzhu.cn/


题目地址:
https://leetcode.com/problems/maximum-subarray/#/description

题目描述

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.

题目大意

找出子数组的最大和。

解题方法

暴力解法

所谓暴力解法,就是找出所有子数组的最大的和。

为了快速求子数组的和,我们有个常用的技巧,就是用个 preSum[i] 数组表示在 i 位置之前的数组的和。即 preSum[i] = sum(num[0]...nums[i])

然后使用两重循环,遍历所有的子数组,子数组和可以用 preSum[j] - preSum[i] 直接求出。

总的时间复杂度是 O(N ^ 2),可以通过。

C++代码如下:

class Solution {
public:
int maxSubArray(vector<int>& nums) {
const int N = nums.size();
vector<int> preSum(N + 1, 0);
for (int i = 0; i < N; ++i) {
preSum[i + 1] = preSum[i] + nums[i];
}
int res = INT_MIN;
for (int i = 0; i < N + 1; ++i) {
for (int j = i + 1; j < N + 1; ++j) {
res = max(res, preSum[j] - preSum[i]);
}
}
return res;
}
};

动态规划

明显的DP方法去解决。

通过构建一个和原长一样长的数组, dp 数组的含义是以 dp[i] 为结尾的最大子数组的和。

状态转移公式:

  1. dp[i] = dp[i - 1] + nums[i] 当 nums[i] >= 0 。
  2. dp[i] = nums[i] 当 nums[i] < 0 。

题目求的最大子数组的和,就是 dp 数组的最大值。

Java 代码如下:

public class Solution {
public int maxSubArray(int[] nums) {
int len = nums.length;
int[] dp = new int[len];
dp[0] = nums[0];
int max = dp[0];
for(int i = 1; i < len; i++){
dp[i] = nums[i] + (dp[i -1] > 0 ? dp[i -1] : 0);
max = Math.max(max, dp[i]);
}
return max;
}
}

二刷,Python解法如下:

class Solution(object):
def maxSubArray(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
if not nums: return 0
N = len(nums)
cur, prev = 0, 0
res = float("-inf")
for i in range(N):
cur = nums[i] + (prev if prev > 0 else 0)
prev = cur
res = max(res, cur)
return res

三刷,C++解法如下:

class Solution {
public:
int maxSubArray(vector<int>& nums) {
const int N = nums.size();
int res = nums[0];
vector<int> dp(N, 0); // 以i为结尾的最大子数组的max subarray.
dp[0] = nums[0];
for (int i = 1; i < N; ++i) {
dp[i] = nums[i] + (dp[i - 1] > 0 ? dp[i - 1] : 0);
res = max(res, dp[i]);
}
return res;
}
};

日期

2017 年 5 月 2 日
2018 年 11 月 19 日 —— 周一又开始了
2020 年 4 月 3 日 —— 这个题是英文版leetcode的每日一题

【LeetCode】53. Maximum Subarray 最大子序和 解题报告(Python & C++ & Java)的更多相关文章

  1. LeetCode 53. Maximum Subarray最大子序和 (C++)

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

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

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

  3. [leetcode]53. Maximum Subarray最大子数组和

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

  4. 53. Maximum Subarray最大子序和

    网址:https://leetcode.com/problems/maximum-subarray/submissions/ 很简单的动态规划 我们可以把 dp[i] 表示为index为 i 的位置上 ...

  5. 【LeetCode】Maximum Subarray(最大子序和)

    这道题是LeetCode里的第53道题. 题目描述: 给定一个整数数组 nums ,找到一个具有最大和的连续子数组(子数组最少包含一个元素),返回其最大和. 示例: 输入: [-2,1,-3,4,-1 ...

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

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

  7. 【LeetCode】26. Remove Duplicates from Sorted Array 解题报告(Python&C++&Java)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 双指针 日期 [LeetCode] https:// ...

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

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

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

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

随机推荐

  1. Can't connect to HTTPS URL because the SSL module is not available. - skipping

    今天用pip3安装第三方库的时候报了这样一个错: Can't connect to HTTPS URL because the SSL module is not available. - skipp ...

  2. 54. Flatten Binary Tree to Linked List

    Flatten Binary Tree to Linked List My Submissions QuestionEditorial Solution Total Accepted: 81373 T ...

  3. ansible-playbook 安装redis 主从

    ansible-playbook 安装redis 主从 手动在测试机上安装一遍redis,最好使用utils下面的install_server.sh安装服务,然后将redis的配置文件和init需要的 ...

  4. Echart显示后端mysql数据

    一.基本思想 1.将数据存储在mysql数据库中 2.后端链接数据库,将数据库中的数据保存为json格式 3.将json格式数据使用ajax传到前端JSP页面中的Echarts 二.实现的关键点 1. ...

  5. 使用WtmPlus低代码平台提高生产力

    低代码平台的概念很火爆,产品也是鱼龙混杂. 对于开发人员来说,在使用绝大部分低代码平台的时候都会遇到一个致命的问题:我在上面做的项目无法得到源码,完全黑盒.一旦我的需求平台满足不了,那就是无解.   ...

  6. 【Reverse】每日必逆0x03

    BUU-刮开有奖 附件:https://files.buuoj.cn/files/abe6e2152471e1e1cbd9e5c0cae95d29/8f80610b-8701-4c7f-ad60-63 ...

  7. Java SSLSocket

    Java SSLSocket JSSE(Java Security Socket Extension)是Sun公司为了解决互联网信息安全传输提出的一个解决方案,它实现了SSL和TSL协议,包含了数据加 ...

  8. 深入理解java动态代理机制

    动态代理其实就是java.lang.reflect.Proxy类动态的根据您指定的所有接口生成一个class byte,该class会继承Proxy类,并实现所有你指定的接口(您在参数中传入的接口数组 ...

  9. Docker 安装 Oracle12c

    为选定需要pull到系统中的数据库镜像 # docker pull sath89/oracle-12c --------sath89/oracle-12c为选定需要pull到系统中的数据库镜像 doc ...

  10. Java Timestamp 类的使用

    很简单,我们可以这样声明 Timestamp ts=new Timestamp(new Date().getTime());这样我们就可以得到时间比较具体的一个类型转换!!! 在开发web应用中,针对 ...