题目描述

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.

解题思路

动态规划思想

以nums数组[-2,1,-3,4,-1]为例

  • dp[0]为-2
  • dp[1] = max(dp[0]+nums[1],1)=max(-2,1)=1
  • dp[2] = max(dp[1]+nums[2],-3)=max(1-3,-3)=-2
  • 当前的sum为dp[i-1]+nums[i], nums[i]最大值
  • 然后将maxSum和sum进行比较,取最大值

Go代码实现

Go代码实现1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
func (a int, b int)int  {
if a>b {
return a
}else{
return b
}
}
func maxSubArray(nums []int) int {
n := len(nums) if n == 0 {
return 0
} if n == 1 {
return nums[0]
大专栏  L53-Maximum-Subarrays="line"> } sums := make([]int, n)
maxSum := nums[0]
sums[0] = nums[0] for i:=1;i<n ; i++ {
sums[i] = max(sums[i-1]+nums[i], nums[i])
maxSum = max(sums[i], maxSum)
}
return maxSum
}

Go代码实现2

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
func maxSubArray(nums []int) int {
n := len(nums) if n == 0 {
return 0
} maxSum := nums[0]
curSum := nums[0] for i:=1;i<n ; i++ {
if curSum<0 {
curSum = nums[i]
}else{
curSum += nums[i]
} if curSum>maxSum {
maxSum = curSum
}
}
return maxSum
}

参考文档

L53-Maximum-Subarray的更多相关文章

  1. [LintCode] Maximum Subarray 最大子数组

    Given an array of integers, find a contiguous subarray which has the largest sum. Notice The subarra ...

  2. 【leetcode】Maximum Subarray (53)

    1.   Maximum Subarray (#53) Find the contiguous subarray within an array (containing at least one nu ...

  3. 算法:寻找maximum subarray

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

  4. LEETCODE —— Maximum Subarray [一维DP]

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

  5. 【leetcode】Maximum Subarray

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

  6. maximum subarray problem

    In computer science, the maximum subarray problem is the task of finding the contiguous subarray wit ...

  7. (转)Maximum subarray problem--Kadane’s Algorithm

    转自:http://kartikkukreja.wordpress.com/2013/06/17/kadanes-algorithm/ 本来打算自己写的,后来看到上述链接的博客已经说得很清楚了,就不重 ...

  8. 3月7日 Maximum Subarray

    间隔2天,继续开始写LeetCodeOj. 原题: Maximum Subarray 其实这题很早就看了,也知道怎么做,在<编程珠玑>中有提到,求最大连续子序列,其实只需要O(n)的复杂度 ...

  9. LeetCode: Maximum Product Subarray && Maximum Subarray &子序列相关

    Maximum Product Subarray Title: Find the contiguous subarray within an array (containing at least on ...

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

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

随机推荐

  1. Prometheus监控系统之入门篇(一)

    1. 简介 Prometheus: (简称Prom)是由SoundCloud开发的开源监控报警系统.是大名鼎鼎的CNCF云原生基金会下的第二大开源项目.具有如下特点: 使用Go语言开发 内置时序数据库 ...

  2. java第三方工具包

    --搜集于网络 1.Apache POI 处理office文档用到的2. IText PDF操作类库 3.Java Base64 Base64编码类库 4.Commons-lang 对应java sd ...

  3. Debian8.8下的VIM的配置文件

    传动们:http://blog.csdn.net/gatieme/article/details/43883261?spm=5176.100239.blogcont47532.3.yXiEuB 感觉挺 ...

  4. Mutation|DNM|

    生命组学 DNA序列改变的分子基础 变异来源 据研究对象,可分为两类mutation:个体上的变异和群体上的变异,群体上的变异是关联研究,eg喝酒人群vs非喝酒人群相比. 造成mutation的三类机 ...

  5. windows10+apache2.4+python3.6部署Django2.2.4项目

    刚从家回来,老师让写专利,就开始准备写,初稿交给老师后,把我说了一顿,我就想着回去改呀,然后...老师找到了我,说是食品院那急需一个展示数据的平台,然我尽快干出来,我也是菜鸟啊,就没单独干过呀,即使是 ...

  6. play framework在eclipse中自动的预编译生成precompiled文件

    一.修改 eclipe 中的启动文件属性eclipse/*.launch 中的最后一条:加入参数 -Dprecompile=yes  将会在启动项目时,进行项目的预编译 (将在项目中生成 precom ...

  7. [LC] 452. Minimum Number of Arrows to Burst Balloons

    There are a number of spherical balloons spread in two-dimensional space. For each balloon, provided ...

  8. hdu1069 Monkey and Banana LIS

    #include<cstdio> #include<iostream> #include<algorithm> #include<queue> #inc ...

  9. Java程序员常用Linux性能分析命令

    性能分析 vmstat 虚拟内存统计 用法 Usage: vmstat [options] [delay [count]] Options: -a, --active active/inactive ...

  10. 乐观锁(Optimistic Lock)

    乐观锁(非阻塞)指不通过锁表来解决并发问题,一般情况下表数据都会加入一个version字段,对该字段进行比较更新来保证数据的一致性. 这里写了个demo,应该可以说明乐观锁的问题. public cl ...