最大子数组:Maximum Subarray

参考来源:Maximum subarray problem

Kadane算法扫描一次整个数列的所有数值,在每一个扫描点计算以该点数值为结束点的子数列的最大和(正数和)。该子数列由两部分组成:以前一个位置为结束点的最大子数列、该位置的数值。因为该算法用到了“最佳子结构”(以每个位置为终点的最大子数列都是基于其前一位置的最大子数列计算得出)时间复杂度O(n)

public class Solution {
public int maxSubArray(int[] nums) {
int res =0 , curSum = 0;
for (int num : nums) {
curSum = Math.max(curSum + num, num);
res = Math.max(res, curSum);
}
return res;
}
}

【数据结构】算法 Maximum Subarray的更多相关文章

  1. 算法:寻找maximum subarray

    <算法导论>一书中演示分治算法的第二个例子,第一个例子是递归排序,较为简单.寻找maximum subarray稍微复杂点. 题目是这样的:给定序列x = [1, -4, 4, 4, 5, ...

  2. 53. Maximum Subarray最大求和子数组12 3(dp)

    [抄题]: Find the contiguous subarray within an array (containing at least one number) which has the la ...

  3. leetCode 53.Maximum Subarray (子数组的最大和) 解题思路方法

    Maximum Subarray  Find the contiguous subarray within an array (containing at least one number) whic ...

  4. LeetCode 53. Maximum Subarray(最大的子数组)

    Find the contiguous subarray within an array (containing at least one number) which has the largest ...

  5. [array] leetcode - 53. Maximum Subarray - Easy

    leetcode - 53. Maximum Subarray - Easy descrition Find the contiguous subarray within an array (cont ...

  6. 动态规划法(八)最大子数组问题(maximum subarray problem)

    问题简介   本文将介绍计算机算法中的经典问题--最大子数组问题(maximum subarray problem).所谓的最大子数组问题,指的是:给定一个数组A,寻找A的和最大的非空连续子数组.比如 ...

  7. 小旭讲解 LeetCode 53. Maximum Subarray 动态规划 分治策略

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

  8. 【LeetCode】053. Maximum Subarray

    题目: Find the contiguous subarray within an array (containing at least one number) which has the larg ...

  9. LN : leetcode 53 Maximum Subarray

    lc 53 Maximum Subarray 53 Maximum Subarray Find the contiguous subarray within an array (containing ...

随机推荐

  1. java动态代理实现与原理详细分析

    关于Java中的动态代理,我们首先需要了解的是一种常用的设计模式--代理模式,而对于代理,根据创建代理类的时间点,又可以分为静态代理和动态代理. 一.代理模式    代理模式是常用的java设计模式, ...

  2. BZOJ.3566.[SHOI2014]概率充电器(概率DP 树形DP)

    BZOJ 洛谷 这里写的不错,虽然基本还是自己看转移... 每个点的贡献都是\(1\),所以直接求每个点通电的概率\(F_i\),答案就是\(\sum F_i\). 把\(F_x\)分成:父节点通电给 ...

  3. python实现的跳点寻路算法(JPS)

    原理参考论文 代码已提交到git(https://github.com/YYRise/find_path/blob/master/jps.py)

  4. PYTOGO之旅——环境搭建

    Go 语言支持以下系统: Linux FreeBSD Mac OS X(也称为 Darwin) Windows 安装包下载地址为:https://golang.org/dl/. 如果打不开可以使用这个 ...

  5. Django模型操作常用方法

    1.Save() 基本方法:object.save() save顾名思义是保存的意思,在django中既可以进行新增也可以进行修改操作.具体判定的算法如下: 1.如果对象的主键属性为一个求值为True ...

  6. springboot 使用 swagger2

    段时间,同事分享了一下 swagger-ui,于是自己尝试了一下.大致的使用过程这里记录一下: 1.添加依赖 <!--swagger-ui--><dependency> < ...

  7. [LeetCode] Encode N-ary Tree to Binary Tree 将N叉树编码为二叉树

    Design an algorithm to encode an N-ary tree into a binary tree and decode the binary tree to get the ...

  8. Servlet 参数

    1.应用参数,在web.xml配置,所有Servlet共用 <context-param> <param-name>driver</param-name> < ...

  9. PCB布局布线

    1.关键芯片的物理位置,明细信号流向,防止关键信号交叉,高速线布线通畅. 2.可装配,可维修,可测试. 3.模拟电路和数字电路分区摆放. 4.疏密有序. 5.原理图应该明确主芯片周边元件的布局要求. ...

  10. Tensorflow Chapter-6

    |--子节点 |--单位节点矩阵:长宽为1,深度不限 |--多维度的二维卷积,和之前的理解不一样:应该是每个通道都要计算在下一层的每个节点上:2*2*3,f=1*1*5 -> 2*2*3*5个参 ...