L53-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] 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 |
func (a int, b int)int { |
Go代码实现2
1 |
func maxSubArray(nums []int) int { |
参考文档
L53-Maximum-Subarray的更多相关文章
- [LintCode] Maximum Subarray 最大子数组
Given an array of integers, find a contiguous subarray which has the largest sum. Notice The subarra ...
- 【leetcode】Maximum Subarray (53)
1. Maximum Subarray (#53) Find the contiguous subarray within an array (containing at least one nu ...
- 算法:寻找maximum subarray
<算法导论>一书中演示分治算法的第二个例子,第一个例子是递归排序,较为简单.寻找maximum subarray稍微复杂点. 题目是这样的:给定序列x = [1, -4, 4, 4, 5, ...
- LEETCODE —— Maximum Subarray [一维DP]
Maximum Subarray Find the contiguous subarray within an array (containing at least one number) which ...
- 【leetcode】Maximum Subarray
Maximum Subarray Find the contiguous subarray within an array (containing at least one number) which ...
- maximum subarray problem
In computer science, the maximum subarray problem is the task of finding the contiguous subarray wit ...
- (转)Maximum subarray problem--Kadane’s Algorithm
转自:http://kartikkukreja.wordpress.com/2013/06/17/kadanes-algorithm/ 本来打算自己写的,后来看到上述链接的博客已经说得很清楚了,就不重 ...
- 3月7日 Maximum Subarray
间隔2天,继续开始写LeetCodeOj. 原题: Maximum Subarray 其实这题很早就看了,也知道怎么做,在<编程珠玑>中有提到,求最大连续子序列,其实只需要O(n)的复杂度 ...
- LeetCode: Maximum Product Subarray && Maximum Subarray &子序列相关
Maximum Product Subarray Title: Find the contiguous subarray within an array (containing at least on ...
- leetCode 53.Maximum Subarray (子数组的最大和) 解题思路方法
Maximum Subarray Find the contiguous subarray within an array (containing at least one number) whic ...
随机推荐
- opencv+tkinter制作HsvMaster(一)
这两天看opencv-python的HSV色彩空间,在写程序时发现用HSV来提取图像区域是件令人恶心的麻烦事.拿阈值分割做个对比,阈值最多也就一两个参数需要调整:但是HSV需要对三个通道调整上下限,也 ...
- 把cifar数据转换为图片
参考 https://gist.github.com/fzliu/64821d31816bce595a4bbd98588b37f5 """ make_cifar10.py ...
- HTTP Status 500 - Request processing failed; nested exception is java.lang.IllegalArgumentException: Control character in cookie value or attribute.
HTTP Status 500 - Request processing failed; nested exception is java.lang.IllegalArgumentException: ...
- Linux_打包文件
将多个文件打包成一个大文件,用tar命令 tar是将多个文件前后连接在一起,tar并不对文件进行压缩 tar -cf 要创建的打包文件名(最后加上.tar) 要打包的文件/列表 c代表创 ...
- OrderValidator
package org.linlinjava.litemall.core.validator; import javax.validation.Constraint; import javax.val ...
- 算法笔记4.3递归 问题 B: 数列
题目描述 编写一个求斐波那契数列的递归函数,输入n 值,使用该递归函数,输出如下图形(参见样例). 输入 输入第一行为样例数m,接下来有m行每行一个整数n,n不超过10. 输出 对应每个样例输出要求的 ...
- vue实现动态绑定class--(boolean)绑定class,点击有,再点击取消
<template> <div :class="{'flag':selected}" @click=clickBtn>xxx</div>< ...
- SpringBoot 将自制的Starter 发布到远程公服
上一篇文章:就是简单的介绍了如何自己制作一个starter ,由于上篇文章只是我个人的笔记,就是将其中重要的部分写出来了,少了其他的基础步骤,但是这个我自己就能看懂,也算不上是一篇好的博客,只能算是笔 ...
- JS UTC 昨天
var birthday = new Date("Jan 01, 1983 01:15:00") var formatDate = function (date) { ...
- TreeMap简介
在Map集合框架中,除了HashMap以外,TreeMap也是常用到的集合对象之一.与HashMap相比,TreeMap是一个能比较元素大小的Map集合,会对传入的key进行了大小排序.其中,可以使用 ...