首页 > 其他分享 >[53]Maximum Subarray

[53]Maximum Subarray

时间:2023-08-17 11:01:45浏览次数:36  
标签:nums max sum Maximum 53 largest Subarray dp subarray

Content

Given an integer array nums, find the subarray with the largest sum, and return its sum.

Example 1:

Input: nums = [-2,1,-3,4,-1,2,1,-5,4]
Output: 6
Explanation: The subarray [4,-1,2,1] has the largest sum 6.

Example 2:

Input: nums = [1]
Output: 1
Explanation: The subarray [1] has the largest sum 1.

Example 3:

Input: nums = [5,4,-1,7,8]
Output: 23
Explanation: The subarray [5,4,-1,7,8] has the largest sum 23.

Constraints:

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

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.

Solution

1. 动态规划

Java

class Solution {
    public int maxSubArray(int[] nums) {
        // 1 <= nums.length <= 10⁵
        // -10⁴ <= nums[i] <= 10⁴
        int n = nums.length;
        // dp[i]表示以下标i结尾的求和最大的子数组的求和的值
        int[] dp = new int[n];
        dp[0] = nums[0];
        int max = dp[0];
        for (int i = 1; i < n; i++) {
            if (dp[i - 1] > 0) {
                dp[i] = dp[i - 1] + nums[i];
            } else {
                dp[i] = nums[i];
            }
            max = Math.max(max, dp[i]);
        }
        return max;
    }
}

标签:nums,max,sum,Maximum,53,largest,Subarray,dp,subarray
From: https://www.cnblogs.com/shea24/p/17637050.html

相关文章

  • 【上传文件时异常】The field file exceeds its maximum permitted size of 1048576 b
    1、背景描述本项目是个springboot项目,需要文件上传,上传的是一个pdf文件,大小是5MB,报错内容如下:Causedby:org.apache.tomcat.util.http.fileupload.FileUploadBase$FileSizeLimitExceededException:Thefieldfileexceedsitsmaximumpermittedsizeof1048576bytes.2......
  • VTK 实例53:网格平滑
    1#include"vtkAutoInit.h"2VTK_MODULE_INIT(vtkRenderingOpenGL2);3VTK_MODULE_INIT(vtkInteractionStyle);45#include<vtkSmartPointer.h>6#include<vtkPolyDataReader.h>7#include<vtkPolyData.h>8#include<vt......
  • hdu 5365
    LCPArrayTimeLimit:4000/2000MS(Java/Others)    MemoryLimit:131072/131072K(Java/Others)TotalSubmission(s):1223    AcceptedSubmission(s):338ProblemDescriptions=s1s2...sn,let suffi=sisi+1...sn bethesuffixstartwith......
  • HDU 5313
    TheshortestproblemTimeLimit:3000/1500MS(Java/Others)  MemoryLimit:65536/65536K (Java/Others)TotalSubmission(s):1484  AcceptedSubmission(s):686ProblemDescriptionInthisproblem,weshouldsolveaninterestinggame.Atfirst,we hav......
  • HDU 5364
    DistributionmoneyTimeLimit:2000/1000MS(Java/Others)  MemoryLimit:65536/65536K(Java/Others)TotalSubmission(s):310  AcceptedSubmission(s):186ProblemDescriptionAFAwanttodistributionhermoneytosomebody.Shedividehermoneyintons......
  • HDU 5366
    ThemookjongTimeLimit:2000/1000MS(Java/Others)  MemoryLimit:65536/65536K(Java/Others)TotalSubmission(s):326  AcceptedSubmission(s):252ProblemDescription![](../../data/images/C613-1001-1.jpg)ZJiaQwanttobecomeastrongman,sohed......
  • Codechef - Longest AND Subarray(位运算)
    题目大意  给定一个正整数N,其序列为[1,2,3,...,N],找到一个长度最大的连续子列,使得其所有元素取与运算的结果为正(最终输出只需要输出最大长度即可)。 思路  刚开始可能并不好注意到,可以举一些小的样例来找规律。比如2:1&2=0,不满足条件,所以只能取长度为1的数组[1]或......
  • E. Maximum Monogonosity
    E.MaximumMonogonosityYouaregivenanarray$a$oflength$n$andanarray$b$oflength$n$.Thecostofasegment$[l,r]$,$1\lel\ler\len$,isdefinedas$|b_l-a_r|+|b_r-a_l|$.Recallthattwosegments$[l_1,r_1]$,$1\lel_1\ler......
  • Maximum execution time of 300 seconds
    我在mysql用phpmyadmin导入数据的时候出现:Fatalerror:Maximumexecutiontimeof300secondsexceededinD:\XXX上网查了很多文章都说是把php.ini里面的 max_execution_time改大就可以,可我改了还是不行。。后来才查出原来是phpmyadmin自己的限制。找到phpmyadmin目录......
  • 【题解】洛谷 P9532 [YsOI2023] 前缀和
    原题链接【LGR-151-Div.2】洛谷8月月赛II&YsOI2023T1解题思路设有一序列a,其中a1 =a2,第k(≥3) 项为前k-1项的前缀和。可以发现前q项分别为第一项的20 倍,20 倍,21 倍,22 倍,23 倍…2q-3 倍,2q-2 倍。扩展到整个序列中,可得第i( ≥ 3)项一定为2i-2a1。......