【LeetCode】解题53:Maximum Subarray

病理组学分析系列教程1:使用Python-histolab进行病理图像预处理,tiles切片自动生成 病理图像tiles自动生成,WSI图像预处理 阅读详情

Problem 53: Maximum Subarray [Easy]

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.

来源:LeetCode

解题思路

基本思想:贪心算法。总是作出使和最大的选择。
变量:1. 当前最大和 2. 全局最大和

具体算法:

  • sum代表当前最大和,max代表全局最大和。
  • 遍历数组,sum对当前元素nums[i]进行累加,与max作比较并更新max,当sum < 0时,说明当前的累计和对后续无正贡献,则将sum清零。

时间复杂度:O( n n n)

题中建议的分治法类似于合并排序,时间复杂度:O( n log ⁡ n n \log n nlogn)

Solution (Java)

class Solution {
    public int maxSubArray(int[] nums) {
        int N = nums.length;
        if(N == 1) return nums[0];
        int max = Integer.MIN_VALUE;
        int sum = 0;
        for(int i = 0; i < N; i++){
            sum += nums[i];
            if(sum > max){
                max = sum;
            }
            if(sum < 0){
                sum = 0;
            }
        }
        return max;
    }
}
基于STM32、HAL库的TLV320AIC3204IRHBR音频接口芯片驱动程序设计 * 开启所有数字部分 */#define TLV320AIC3204_REG_LEFT_HEADPHONE_OUT_VOLUME 0x0C /* 左声道耳机输出音量 */#define TLV320AIC3204_REG_DIGITAL_AUDIO_PATH_CONTROL 0x05 /* 数字音频路径控制 */#define TLV320AIC3204_REG_ANALOG_HP_FILTER_CONTROL 0x34 /* 模拟耳机滤波器控制 *//* 最大音量 */ 阅读详情

相关推荐

ASP.NET MVC5 C# Entity Framework 连接 Oracle 数据库

ASP.NET MVC5 C# Entity Framework 连接 Oracle 数据库创建数据库安装 Entity Framework配置数据库连接字符串添加实体模型类添加数据库上下文类添加控制器类运行测试 创建数据库 创建【全局数据库名】为 demodb 的Oracle数据库,用 SYSTEM 用户连接成功后创建【用户名】为 DJIANG 的用户,并授予该用户 DBA 角色 安装 Enti...

idjiang的博客 2541

Maximum Subarray leetcode java

题目: Find the contiguous subarray within an array (containing at least one number) which has the largest sum. For example, given the array [−2,1,−3,4,−1,2,1,−5,4], the contiguous subarray [4,−1...

weixin_34399060的博客 127

光耦CTR参数实战:如何避免误触发与功耗陷阱(附计算公式)

本文深入解析光耦CTR参数在电路设计中的关键作用,探讨如何平衡误触发风险与功耗表现。通过TLP521、PC817等常见型号的实测数据对比,提供不同工作状态下的选型策略和计算公式,帮助工程师优化设计,提升系统稳定性和能效。

2a4s6d8f0g的博客 328

leetcode解题报告:53. Maximum Subarray

题意:Find the contiguous subarray within an array (containing at least one number) which has the largest sum. For example, given the array [-2,1,-3,4,-1,2,1,-5,4], the contiguous subarray [4,-1,2,1]

wengyunpeng的博客 351

多模态融合相关概念

多模态图像融合

weixin_43921949的博客 7393

HITS算法详解及代码实现

占坑

蓁蓁尔的博客 7468

Leetcode解题之路(golang版):53. 最大子序和(Maximum Subarray)

53. 最大子序和 给定一个整数数组 nums ,找到一个具有最大和的连续子数组(子数组最少包含一个元素),返回其最大和。 示例: 输入: [-2,1,-3,4,-1,2,1,-5,4], 输出: 6 解释: 连续子数组 [4,-1,2,1] 的和最大,为 6。 进阶: 如果你已经实现复杂度为 O(n) 的解法,尝试使用更为精妙的分治法求解。 解不出来法一: 空间复杂度:$O(1)$ 时间复...

myz123321的博客 555

Altair HyperWorks 2022.3.0 Suite下载

Altair HyperWorks开发团队提供了新的一流技术,以设计和优化高性能、高效和创新的产品,并发布了HyperWorks 2021.2套件。与所有新版本一样,这个版本包含了新特性、增强功能,并修复了前一个版本中用户报告的各种问题,提供了总体上更稳定的体验。链接: https://pan.baidu.com/s/1rYietmQLQUboHiJzYoE9WQ?pwd=m9yh 提取码: m9yh 复制这段内容后打开百度网盘手机App,操作更方便哦。

weixin_48469365的博客 2165

【leetcode】53. 最大子序和(Maximum Subarray)

题目描述 【leetcode】53. 最大子序和(Maximum Subarray) 给定一个整数数组 nums ,找到一个具有最大和的连续子数组(子数组最少包含一个元素),返回其最大和。 第一次解答 思路: 暴力法。 遍历所有可能的组合,起始点i从0开始到nums.size()-1 遍历从所有起始点开始,到结束点j处的累加和,返回最大的那个。 执行次数=1+2+3+…+n,等差数列,所以O(...

Liber-coder的博客 177

华为[ENSP]OSPF的配置实例(单区域+多区域)

华为[ENSP]OSPF的配置实例(单区域+多区域)

weixin_62594100的博客 2万+

uboot引导vxworks6.9(T4240) 启动

vxworks内核配置下搜索RAM_LOW_ADRS, 值为0x01000000, 是vxworks下载启动的地址,uboot启动时会用到。vxworks6.9 t4240用的是32地址空间,uboot下用的是64位地址空间,去uboot下修改law物理地址空间为32位。vxworks下flash基地址为 0xff000000, 将vx下的地址修改为0xe0000000,和uboot下地址一致。uboot下flash基地址为0xe000000,vxworks内核配置为build-in 符号表。

珂珂可爱多 3251

【LeetCode & 剑指offer刷题】分治法题2:42 连续子数组的最大和(53. Maximum Subarray)...

【LeetCode &amp; 剑指offer刷题】分治法题2:42 连续子数组的最大和(53. Maximum Subarray) 【LeetCode &amp; 剑指offer 刷题笔记】目录(持续更新中...) 53. Maximum Subarray Given an integer array nums, find the con...

技术改变世界 552

Outlook Express 6 (OE6) for Win7/Win8/Win10

英文版 Outlook Express6.0, 兼容Win7/Win10/WinXP,解压就可用。

LeetCode 53. Maximum Subarray 题解

本题是动态规划的经典问题,主要考察对状态定义和状态转移方程的理解。动态规划:代码清晰易懂,逻辑明确,但需要 O(n) 的空间。动态规划(空间优化):空间复杂度为 O(1),更高效,适用于大规模数据。在实际应用中,空间优化的方法通常是首选,因为它不仅空间效率高,而且代码简洁。这种方法不仅适用于最大子数组和问题,还可以应用于许多其他需要动态规划解决的问题,例如最大乘积子数组、最长递增子序列等。掌握动态规划的思想,对于解决这类问题非常重要。

cannonjinx的博客 4403

锂电池充电器电路设计 里面有电路

锂电池充电器电路设计 里面有电路 3.7V升压到5V电路设计。

SAP B1中文培訓教材_SDK

公司内部培训资料,对开发工具 ,DIAPI, UIAPI,Add-ON有详细解说

leetcode解题之53 # Maximum Subarray Java版

leetcode解题之53 # Maximum Subarray Java版

mine_song的博客 1747

vb 6.0并口开发操作实例

vb6并口开发,简体中文,试例内有inpout32.dll动态链接库

【LeetCode】53. Maximum Subarray 解题报告(Python)

【LeetCode】53. Maximum Subarray 解题报告(Python)题目描述解法1:动态规划解法2:分治法分治法和动态规划区别 题目地址:https://leetcode.com/problems/maximum-subarray/ 题目描述 Given an integer array nums, find the contiguous subarray (containing...

ttinch的博客 256

【Leetcode 53】 Maximum Subarray - EASY

【Leetcode 53】Maximum Subarray - EASY题目思路题解反思复杂度分析思路反思 题目 Given an integer array nums, find the contiguous subarray (containing at least one number) which has the largest sum and return its sum. 给定整数数组...

weixin_43621681的博客 316

leetcode 53 Maximum Subarray 详细解答

leetcode 53 Maximum Subarray 详细解答 解法1 动态规划: 举例说明:[−2,2,−1,4,−1,2,1,−5,4][-2, 2, -1, 4, -1, 2, 1, -5, 4][−2,2,−1,4,−1,2,1,−5,4] (1):第一个组合[−2]到以第一个数字结尾的最大连续子数组,则最大值−2(2):第二个组合[−2,2]到以第二个数字结尾的最大连续子数组,当前最大值2(3):第三个组合[−2,2,−1]到以第三个数字结尾的最大连续子数组,当前最大值为2+(−1)=1(

qq_22210659的博客 314

Leetcode 53. Maximum Subarray 解题报告

题目链接 Leetcode 53. Maximum Subarray Find the contiguous subarray within an array (containing at least one number) which has the largest sum. For ...

chunhuang2917的博客 186

初刷leetCode--数组系列--Maximum Subarray(连续最大子序列)

前言 今天正式进入到leetCode的刷题中,先根据数据结构划分,从与数组且Easy的问题开始。对于选题,我们没必要每一题都做,可以先阅读题目,觉得思路清晰简单的就可以直接跳过,例如开始的 Two Sum, Remove Duplicates from Sorted Array,Remove Element, Search Insert Position思路和实现上都没有什么难度,可以直接跳过...

weixin_41726080的博客 511

[算法分析与设计] leetcode 每周一题: 053. Maximum Subarray

题目链接:  53. Maximum Subarray 题目大意: 给定整型数组 A, 找出 A 的(连续)子数组的和的最大值 ; 例如: 给定数组 [-2,1,-3,4,-1,2,1,-5,4], 则输出应为: 6 ; ( 对应子数组 [4,-1,2,1] ) 解题过程: (注: 因为这种类型的题目以前不知道什么时候做过, 所以以下尽量回忆一下当时的思

sinat_37893507的博客 258

Leetcode 53:Maximum Subarray (DP基础)

题目描述: 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 Exp...

Just do it ! 432

leetcode 53 Maximum Subarray

leetcode 53 Maximum Subarray 最大子序列和问题是用于讲解分治策略的一个经典例题,这个例题可以帮助我们很好的理解分治策略。但对于这个问题,分治策略并不是最高效的算法。 清爽版入口 问题描述 给定一个整数数组 nums ,找到其中一个具有最大和的连续子数组(子数组最少包含一个元素),返回其最大和。 示例: 输入: [-2,1,-3,4,-1,2,1,-5,4], 输出: ...

csdnofhaner的博客 266

【LeetCode-Java实现】53. Maximum Subarray

53. Maximum Subarray题目描述解题思路实现代码 题目描述 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,...

IT Crowd的博客 339

LeetCode: 53. Maximum Subarray(Easy)

1. 原题链接 https://leetcode.com/problems/maximum-subarray/discuss/ 2. 题目要求 给定一个整型数组,返回其子串之和的最大值 例如,[-2,1,-3,4,-1,2,1,-5,4]中,[4,-1,2,1]可以构成最大子串之和6 3. 解题思路 对数组进行一次遍历,每次加入一个元素前,先判断其加入后累加和与该元素的大小,...

aysk1112的博客 143

Leetcode Algorithm #53 Maximum Subarray

Leetcode Algorithm #53 Maximum Subarray 题目内容 Given an integer array nums, find the contiguous subarray&nbsp;(containing at least one number) which has the largest sum and return its sum. Example: ...

VanciorH的博客 328
上一篇: 【LeetCode】解题48:Rotate Image
下一篇: 【LeetCode】解题54:Spiral Matrix
Fayedy
博客等级 码龄11年 2粉丝 57原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值