首页 > 其他分享 >LeetCode[53]MaximumSubarray

LeetCode[53]MaximumSubarray

时间:2023-08-18 14:35:42浏览次数:39  
标签:rSum int max nums 53 MaximumSubarray iSum lSum LeetCode

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.

Related Topics
  • 数组
  • 分治
  • 动态规划

  • 标签:rSum,int,max,nums,53,MaximumSubarray,iSum,lSum,LeetCode
    From: https://www.cnblogs.com/shea24/p/17640407.html
  • 相关文章

    • LeetCode[42]TrappingRainWater
      ContentGivennnon-negativeintegersrepresentinganelevationmapwherethewidthofeachbaris1,computehowmuchwateritcantrapafterraining. Example1:Input:height=[0,1,0,2,1,0,1,3,2,1,2,1]Output:6Explanation:Theaboveelevationmap......
    • LeetCode[32]LongestValidParentheses
      ContentGivenastringcontainingjustthecharacters'('and')',returnthelengthofthelongestvalid(well-formed)parenthesessubstring. Example1:Input:s="(()"Output:2Explanation:Thelongestvalidparentheses......
    • LeetCode[10]RegularExpressionMatching
      ContentGivenaninputstrings andapatternp,implementregularexpressionmatchingwithsupportfor'.'and'*'where:'.'Matchesanysinglecharacter.​​​​'*'Matcheszeroormoreoftheprecedingelement.T......
    • LeetCode 392.判断子序列
      1.题目:给定字符串 s 和 t ,判断 s 是否为 t 的子序列。字符串的一个子序列是原始字符串删除一些(也可以不删除)字符而不改变剩余字符相对位置形成的新字符串。(例如,"ace"是"abcde"的一个子序列,而"aec"不是)。进阶:如果有大量输入的S,称作S1,S2,...,Sk其中k>=10亿,你需要......
    • #yyds干货盘点# LeetCode程序员面试金典:存在重复元素 II
      题目:给你一个整数数组 nums 和一个整数 k ,判断数组中是否存在两个 不同的索引 i 和 j ,满足 nums[i]==nums[j] 且 abs(i-j)<=k 。如果存在,返回 true ;否则,返回 false 。 示例 1:输入:nums=[1,2,3,1],k=3输出:true示例2:输入:nums=[1,0,1,1],k=1输出......
    • #yyds干货盘点# LeetCode程序员面试金典:组合和四
      1.简述:给你一个由 不同 整数组成的数组 ,和一个目标整数 。请你从 中找出并返回总和为 的元素组合的个数。numstargetnumstarget题目数据保证答案符合32位整数范围。 示例1:输入:nums=[1,2,3],target=4输出:7解释:所有可能的组合为:(1,1,1,1)(1,1,2)(1,2,1)......
    • 【leetcode】1.two sum
      第一题给我干懵了...想达到这个要求把我脑壳都想痛了...Follow-up:CanyoucomeupwithanalgorithmthatislessthanO(n2)timecomplexity?一开始想过用map,但是不能解决重复key的问题。然而我用sort,这个时间复杂度也不好确定。假装我只用了O(n)!(搓手手)(溜走)xxxclassSol......
    • LeetCode 1035.不相交的线
      1.题目:在两条独立的水平线上按给定的顺序写下 nums1 和 nums2 中的整数。现在,可以绘制一些连接两个数字 nums1[i] 和 nums2[j] 的直线,这些直线需要同时满足满足: nums1[i]==nums2[j]且绘制的直线不与任何其他连线(非水平线)相交。请注意,连线即使在端点也不能相交:每个数字只......
    • LeetCode 718.最长重复子数组
      1.题目:给两个整数数组 nums1 和 nums2 ,返回 两个数组中 公共的 、长度最长的子数组的长度 。 示例1:输入:nums1=[1,2,3,2,1],nums2=[3,2,1,4,7]输出:3解释:长度最长的公共子数组是[3,2,1]。示例2:输入:nums1=[0,0,0,0,0],nums2=[0,0,0,0,0]输出:52.代码:classSo......
    • LeetCode 1143.最长公共子序列
      1.题目:给定两个字符串 text1 和 text2,返回这两个字符串的最长 公共子序列 的长度。如果不存在 公共子序列 ,返回 0 。一个字符串的 子序列 是指这样一个新的字符串:它是由原字符串在不改变字符的相对顺序的情况下删除某些字符(也可以不删除任何字符)后组成的新字符串。例如,"......