#53 Maximum Subarray最大子串和

在Ubuntu 20.04 x86_64主机上为ARM aarch64目标交叉编译libffi:从源码到动态库的完整实践 本文详细介绍了在Ubuntu 20.04 x86_64主机上为ARM aarch64目标交叉编译libffi的完整实践过程。从搭建交叉编译环境、获取源码到配置编译参数,逐步指导开发者解决常见问题,如头文件错误和符号链接问题,最终生成可用的动态库。特别适合嵌入式开发者在资源有限的ARM平台上部署Python等依赖libffi的软件环境。 阅读详情

题目:

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] has the largest sum = 6.

题解:

比较经典的一个问题,是学习的好材料。

解1:穷举,O(n²)复杂度,意料中的超时。

public class Solution {
    public int maxSubArray(int[] nums) {
        int subsum;
        int maxsum=nums[0];//注意这里不是int maxsum=<strong>0</strong>;
        int len=nums.length;
        for(int i=0;i<len;i++){
            subsum=0;
            for(int j=i;j<len;j++)
            subsum+=nums[i];
            maxsum=Math.max(subsum,maxsum);
        }
        return maxsum;
    }
}

解2:&&Kadane算法 

这是我的解法。和Kadane算法 相似有没有,很激动有没有!由于相似,所以放在一起说明了。

首先有这样的结论,对于nums[1,...,n],如果nums[i,...,j]是满足和最大的子串,那么对于任意的i=<k<=j,有nums[i,...,k]的和大于0。利用一个数组sum[i]记录局部子串和。遍历数组,对于i,若sun[i-1]>0,则将nums[i]加入之前子串,并记录子串和sum[i]=sun[i-1]+nums[i],否则nums[i]单开子串,记录子串和sum[i]=nums[i]。最后维护一个全局最大值max,即使返回的最大子串和。

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

Kadane算法 :

原理:将数组从左到右分割为若干子串,使得除了最后一个子串之外,其余子串的各元素之和小于0,且对于所有子串nums[i...j]和任意k(i<=k<j),有nums[i...k]的和大于0。满足条件的和最大子串,只能是上述某个子串的前缀,而不可能跨越多个子串。

原理详细可参考:http://blog.youkuaiyun.com/joylnwang/article/details/6859677

执行流程:从头到尾遍历目标数组,将数组分割为满足上述条件的子串,同时得到各子串的最大前缀和,然后比较各子串的最大前缀和,得到最终答案。

以array={-2, 1, -3, 4, -1, 2, 1, -5, 4}为例,通过遍历,可以将数组分割为如下3个子串(-2),(1,-3),(4,-1,2,1,-5,4),这里对于(-2)这样的情况,单独分为一组。各子串的最大前缀和为-2,1,6,所以目标串的最大子串和为6。

public class Solution {
    public int maxSubArray(int[] nums) {
          int max_ending_here = 0;
          int max_so_far = nums[0];
          
          for(int i = 0; i < nums.length; i++){  
              if(max_ending_here < 0) 
                   max_ending_here = 0;  
              max_ending_here += nums[i];  
              max_so_far = Math.max(max_so_far, max_ending_here);   
         }  
         return max_so_far; 
     }
}


解3:分治法:

最大子串和的区间有以下三种情况(low,high分别为左右边界,mid为(low+high)/2):(1) 区间完全在 A[low,mid-1];(2) 区间完全在 A[mid+1,high];(3) 区间包含有 A[mid],等价于从中间元素开始往左累加的最大值 + 从中间元素开始往右累加的最大值

public class Solution {
    public int maxSubArray(int[] nums) {
           return divide(nums, 0, nums.length-1); 
      }
    public int divide(int nums[], int low, int high){  
          if(low == high)
              return nums[low];  
          if(low == high-1)  
              return Math.max(nums[low]+nums[high], Math.max(nums[low], nums[high]));
             
         int mid = (low+high)/2;  
         int lmax = divide(nums, low, mid-1);  
         int rmax = divide(nums, mid+1, high); 
         
         int mmax = nums[mid];  
         int tmp = mmax;  
         for(int i = mid-1; i >=low; i--){  
             tmp += nums[i];  
             if(tmp > mmax)
                 mmax = tmp;  
         }  
         tmp = mmax;  
         for(int i = mid+1; i <= high; i++){  
             tmp += nums[i];  
             if(tmp > mmax)
                 mmax = tmp;  
         }  
         return Math.max(mmax, Math.max(lmax, rmax));  
     }
}



【信息科学与工程学】【数据科学】数据科学领域 第六篇 算法设计 07 堆、栈、表、矩阵、集合、队列、数组、字符串、树、图、动态规划、结构体、元组的算法01 当前段必须包含所有字符的最后出现位置。1. 字符串分割成最多片段2. 基因序列功能域划分3. 文本段落划分4. 音频声道分离5. 图像色彩分区6. 社交网络社群划分7. 蛋白质结构域8. 音乐乐章分段9. 代码模块划分10. 章节划分特征:一次扫描,贪心确定边界。 阅读详情

相关推荐

[代码开源]基于DistFlow的IEEE33和IEEE69节点最优潮流模型(OPF)实现与性能分析

本文详细介绍了基于DistFlow的IEEE33和IEEE69节点最优潮流模型(OPF)的实现与性能分析。通过代码开源,展示了如何从零搭建电力系统优化模型,解决配电网中的发电成本最小化问题。文章涵盖数据准备、模型实现细节、性能优化建议及常见问题解答,为电力系统研究者提供实用参考。

weixin_29103191的博客 709

[Leetcode] Minimum Size Subarray Sum 最短子串和

Minimum Size Subarray Sum Given an array of n positive integers and a positive integer s, find the minimal length of a subarray of which the sum ≥ s. If there isn't one,...

weixin_34390996的博客 130

Modbus 通信协议详解

Modbus是由 Modicon(现为施耐德电气公司的一个品牌)在 1979 年发明的,是全球第一个真正用于工业现场的总线协议。ModBus网络是一个工业通信系统,由带智能终端的可编程序控制器和计算机通过公用线路或局部专用线路连接而成。其系统结构既包括硬件、亦包括软件。它可应用于各种数据采集和过程监控。为更好地普及和推动Modbus在基于以太网上的分布式应用,目前施耐德公司已将Modbus协议的所有权移交给 IDA(,分布式自动化接口)组织,并成立了 Modbus-IDA 组织。

Projectsauron 的博客 7万+

[Leetcode] 560. Subarray Sum Equals K 解题报告

题目: Given an array of integers and an integer k, you need to find the total number of continuous subarrays whose sum equals to k. Example 1: Input:nums = [1,1,1], k = 2 Output: 2 No

魔豆(Magicbean)的博客 392

STM32无线遥控小车的设计

基于SPI通信的无线摇杆小车的设计

weixin_69414084的博客 2934

大学物理第二章 质点动力学详解

大学物理第二章 质点动力学概念详解

weixin_63026364的博客 1万+

Storm入门例子

import java.util.Map; import java.util.Random; import org.apache.storm.spout.SpoutOutputCollector; import org.apache.storm.task.TopologyContext; import org.apache.storm.topology.OutputFieldsDeclarer;

老李2009 473

Visio 2013安装遇阻?手把手教你搞定注册表权限错误1402(附64位系统特殊处理)

本文详细解析了Visio 2013安装过程中常见的注册表权限错误1402问题,特别是在64位系统下的特殊处理方法。通过分步指导如何修改注册表权限、处理Wow6432Node分支以及使用高级工具如subinacl,帮助用户顺利完成安装并避免常见连带问题。

t8u9v0w1x的博客 546

Leetcode 53. Maximum Subarray 最大子序和

Leetcode 53. Maximum Subarray 最大子序和 标签: Leetcode 题目地址:https://leetcode-cn.com/problems/maximum-subarray/ 题目描述 给定一个整数数组 nums ,找到一个具有最大和的连续子数组(子数组最少包含一个元素),返回其最大和。 示例: 输入: [-2,1,-3,4,-1,2,1,-5,4],...

CoderWangSon 306

2.2 保姆级教程:手把手带你构建第一个 LangGraph 应用

本文是一份保姆级教程,手把手教你构建第一个LangGraph应用——"智能工具助手"Agent。教程分为8个步骤:从环境准备到工具定义(使用Tavily搜索API和自定义计算器),再到定义图状态、创建决策节点和执行节点,最后连接工作流并编译运行。通过这个项目,你将学会如何让Agent智能选择工具(搜索或计算),掌握LangGraph开发的基本流程,为构建更复杂系统打下基础。关键点包括状态管理、节点设计和条件路由的实现。

yonggeit的博客 3117

基于Python的京东商品价格监控与降价提醒系统设计与实现

本文详细介绍了如何使用Python构建一个完整的京东商品价格监控与降价提醒系统。系统通过爬虫技术实时抓取京东商品价格信息,结合数据存储与分析,实现价格波动监控和智能降价提醒功能。文章涵盖了系统设计、技术选型、代码实现以及部署方案,为电子商务价格监控领域提供了一个实用的解决方案。关键词:Python爬虫、京东API、价格监控、降价提醒、自动化。

2201_76125261的博客 405

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

uniapp [全端兼容] - 实现全景图Vr 720°全景效果查看预览功能,3D全景图流畅不卡顿渲染+手势拖拽+悬浮工具按钮,uniAPP实现vr看720度全景效果示例代码(H5小程序APP全兼容)

uniapp显示720度全景图,720°全景图显示,在h5中展示720°全景图,浏览全景图,基于uniApp实现vr全景预览,加载全景图,uni-app全景预览插件,UNI-APP实现3D全景图,实现720°全景图展示,uniapp全景视频播放,uniapp项目实现vr720度全景图片预览,在网页上展示760全景图片和视频,全景图场景切换,UNIAPP全景VR图,uniapp加载720全景图_uniapp怎么加载vr图片,uniapp实现vr全景看房,uni-app实现全景效果,加载很慢卡怎么办,实现全景预

🏆 前端领域优质创作者 1万+

【LeetCode】53. Maximum Subarray

问题描述https://leetcode.com/problems/maximum-subarray/#/descriptionFind the contiguous subarray within an array (containing at least one number) which has the largest sum.For example, given the array [-2,

zgljl2012的专栏 2123

lwip-2.0.3

lwip是瑞典计算机科学院(SICS)的Adam Dunkels 开发的一个小型开源的TCP/IP协议栈。实现的重点是在保持TCP协议主要功能的基础上减少对RAM 的占用。

数学建模模型总结

这是本人在两年内准备参加数学建模竞赛搜集到的模型资料,已经经过了本人的筛选和整理,希望能够对准备参加数学建模竞赛的学弟学妹们提供一些帮助!

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

题目分析: 这一题是让我们找相加最大的子串,这个是属于动态规划的一类题目,在循环的时候我们每次循环都找出以当前位置结束的最大值,再用一个max_sum记录最大的并返回。 代码说明: next_sum = max(next_sum + nums[i], nums[i])是比较当前数值大,还是加上之前的最大字串数值大,即找出以当前位置结束的最大值。 测试代码 class Solution: d...

L141210113的专栏 668

TMS_WebGMaps_1.9.8.0_Full_Source

TMS_WebGMaps_1.9.8.0_Full_Source 全源码 delphi cbuilder

[leetcode]#53. Maximum Subarray

题目是说,给你一串数,有正有负 要求出最大的连续子串。 其实就是标准的动态规划问题: 随着遍历这个数组,在到每一个位置的时候,弄一个局部最大L值,代表以当前位置为结尾的最大子串,比如说我遍历到第i个,那么以第i个为结尾的最大子串就是我们要求的L。 比如这个题目: -2 , 1, −3,4,−1,2,1,−5,4 位置0,L=x=-2,没得选 位置1,要以x=1为结尾的最大的,那肯

小草莓的博客 224

efm32 入门.pdf

本PDF共计22页,介绍的是操作驱动API,下载与否自己斟酌。

53 最大子串和Maximum Subarray

分治法python def maxSub(nums,left,right): if left==right: return nums[left] mid = int((left+right)/2) print("mid=",mid) leftmax = maxSub(nums,left, mid) rightmax = maxSub(nums,mid+1,right) mid_left_sum = 0 mid_right_sum = 0

纪大福 148

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

LawFile的博客 263

leetcode 53. Maximum Subarray(python)

描述 iven 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 yo

王大呀呀的博客 340

Leetcode No.53 Maximum Subarray

  参考资料:   考点:子串 & 动态规划 & [题干] 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.   1. 心路历程   这道题非常经典,蕴含的思想也是精巧无比。   2. 正解   简单来说官解就是...

思念殇千寻的博客 294

week17-leetcode #53-Maximum Subarray

week17-leetcode #53-Maximum Subarray链接:https://leetcode.com/problems/maximum-subarray/description/QuestionFind the contiguous subarray within an array (containing at least one number) which has the lar

perdon123123的博客 385

LeetCode #53: Maximum Subarray

LeetCode Algorithm #53: Maximum Subarray 题解

branch379的博客 326

Leetcode Maximum Subarray python 学习动态规划

其实就是标准的动态规划问题: 随着遍历这个数组,在到每一个位置的时候,弄一个局部最大L值,代表以当前位置为结尾的最大子串,比如说我遍历到第i个,那么以第i个为结尾的最大子串就是我们要求的L。 比如这个题目: -2 , 1, −3,4,−1,2,1,−5,4 位置0,L=x=-2,没得选 位置1,要以x=1为结尾的最大的,那肯定不要带上之前的-2,只要1就好L=x=1 位置2,因为本身x=-3,加上...

润泽的博客 270

#219 Contains Duplicate II

题目: Given an array of integers and an integer k, find out whether there there are two distinct indices i and j in the array such that nums[i] = nums[j] and the difference between i and j is a

private_void的博客 455

#101 Symmetric Tree

题目: Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). For example, this binary tree is symmetric: 1 / \ 2 2 / \ / \ 3 4 4 3 Bu

private_void的博客 441
上一篇: int与integer的区别
下一篇: #169 Majority Element
private_void
博客等级 码龄15年 3粉丝 33原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值