给你一个整数数组 nums ,请你找出一个具有最大和的连续子数组(子数组最少包含一个元素),返回其最大和。

子数组 是数组中的一个连续部分。

示例 1:

输入:nums = [-2,1,-3,4,-1,2,1,-5,4]
输出:6
解释:连续子数组 [4,-1,2,1] 的和最大,为 6 。

示例 2:

输入:nums = [1]
输出:1

示例 3:

输入:nums = [5,4,-1,7,8]
输出:23

提示:

  • 1 <= nums.length <= 105
  • -104 <= nums[i] <= 104

常规dp的时间复杂度为O(n),要是比赛可能会超时,因为这道题更适合分治

 1 class Solution {
2 public:
3 int dp[100005]; //dp[i]表示nums[i]附近的最大子数组和
4 int maxSubArray(vector<int>& nums) {
5 memset(dp,0,sizeof(dp));
6 dp[0]=nums[0];
7 int mmax=dp[0];
8 for (int i=1;i<nums.size();++i){
9 dp[i]=max(nums[i],dp[i-1]+nums[i]); //若nums[i]>dp[i-1]+nums[i]说明添加nums[i]为负收益,从nums[i]开始重新计算最大和
10 mmax=max(dp[i],mmax);
11 }
12 return mmax;
13 }
14 };

力扣53. 最大子数组和(dp)的更多相关文章

  1. 【力扣】两个数组的交集 II

    给定两个数组,编写一个函数来计算它们的交集. 示例 1: 输入:nums1 = [1,2,2,1], nums2 = [2,2]输出:[2,2]示例 2: 输入:nums1 = [4,9,5], nu ...

  2. 力扣350. 两个数组的交集 II

    原题 1 class Solution: 2 def intersect(self, nums1: List[int], nums2: List[int]) -> List[int]: 3 di ...

  3. LeetCode HOT 100:最大子数组和

    题目:53. 最大子数组和 题目描述: 给你一个整数数组,在该数组的所有子数组中,找到一个子数组中所有元素相加和最大,返回这个最大的和.子数组就是一个数组中,由一个或几个下标连续的元素,组成的小数组, ...

  4. 力扣算法JS LC 59-螺旋矩阵2,LC 152-乘积最大子数组

    LC 59-螺旋矩阵2 给你一个正整数 n ,生成一个包含 1 到 n2 所有元素,且元素按顺时针顺序螺旋排列的 n x n 正方形矩阵 matrix . 示例 1: 输入:n = 3输出:[[1,2 ...

  5. Leetcode之动态规划(DP)专题-53. 最大子序和(Maximum Subarray)

    Leetcode之动态规划(DP)专题-53. 最大子序和(Maximum Subarray) 给定一个整数数组 nums ,找到一个具有最大和的连续子数组(子数组最少包含一个元素),返回其最大和. ...

  6. [leetcode]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. 力扣152,53题,最大子序列求和and积

    本内容为最大子序列的求和和求积.采用DP的思路, 当前值加上小于之前值,则从该节点重新算起. 这个代码只能返回其结果值,但不能返回最后的子序列(待修改). class Solution: def ma ...

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

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

  10. LeetCode(力扣)——Search in Rotated Sorted Array2 搜索旋转排序数组 python实现

    题目描述: python实现 Search in Rotated Sorted Array2 搜索旋转排序数组   中文: 假设按照升序排序的数组在预先未知的某个点上进行了旋转. ( 例如,数组 [0 ...

随机推荐

  1. AKS for Gitpod

    ------实习期间做的一个小项目 距今一个多月两个月----------------- 1.First Week local k8s 命令行:kubectl 运行Kubernetes: kind,m ...

  2. 代码随想录算法训练营day09 | leetcode 28. 实现 strStr()

    LeetCode 28. 实现 strStr() 牢记一点:next[i] 元素表示[0,i]子串的最长相等前后缀个数,也是模式串与主串匹配不相等时模式串的下一个比较索引 分析1.0 前缀是指不包含最 ...

  3. vue弹窗后如何禁止滚动条滚动

    toggleBody(isPin){ if(isPin){ document.body.style.height = '100vh' document.body.style['overflow-y'] ...

  4. 在线设计器 DesignO 的分析

    需求分析 现有POD网站的在线编辑器不是很好用. 可配置性不强,素材无法在后台实现管理 可扩展性不强,无法应用于多个行业,比如包装.服装 产品分析 官方网站:https://www.designnbu ...

  5. LeetCode-386 字典序排数

    来源:力扣(LeetCode)链接:https://leetcode-cn.com/problems/lexicographical-numbers 题目描述 给你一个整数 n ,按字典序返回范围 [ ...

  6. python的常见问题解决

    1.python库下载安装慢:用清华镜像 pip install -i https://pypi.tuna.tsinghua.edu.cn/simple --trusted-host pypi.tun ...

  7. oracle 设置用户永不过期

    一.查看用户的proifle是哪个,一般是default SELECT username,PROFILE FROM dba_users; 二.查看指定概要文件(如default)的密码有效期 SELE ...

  8. LCD1602液晶屏(续)

    从前面的分析中知道,在HD44780控制芯片忙的时候,是不能对其进行写入操作的,所以在写入指令或数据时都需要进行判忙的操作,其时序如下图所示(8位数据模式). 从上图中可看到,当HD44780在执行内 ...

  9. Prometheus监控操作

    一.架构说明 ➢ Prometheus Server:Prometheus 生态最重要的组件,主要用于抓取和存储时间序列数据, 同时提供数据的查询和告警策略的配置管理: ➢  Alertmanager ...

  10. Java虚拟机中 -XX:+PrintFlagsFinal与XX:+PrintCommandLineFlags 中MaxHeapSize的值不同的原因

    size_t CollectorPolicy::compute_heap_alignment() { // The card marking array and the offset arrays f ...