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 ...
随机推荐
- 浙江省赛 ZOJ - 4033
题意: 第一行给出T代表有几个测试样例, 第二行给出n代表有几个人, 第三行给出一个由0和1组成的字符串,0代表女生,1代表男生. 并且第i个人有i个宝石. 现在要把这些人分为四组,G1 G2 两组是 ...
- lr cc安装后提示“内部出现错误,无法创建文件夹”的解决办法
好多人在使用lr过程中提示“内部出现错误,无法创建文件夹”,今天MacW小编给大家带来了解决的方法,一起来看看吧! 1.此问题主要是用户权限的问题. 下载这个shell 脚本,此 shell 脚本可自 ...
- [Algo] 306. Check If Linked List Is Palindrome
Given a linked list, check whether it is a palindrome. Examples: Input: 1 -> 2 -> 3 -> 2 ...
- 37)智能指针(就是自动delete空间)
1)问题引入: 在java或者在C++中,一旦你new一个东西,那么必然有一个delete与之对应,比如: int main() { int* p= new int(): *p=: delete p: ...
- PAT甲级——1005.SpellItRight(20分)
Given a non-negative integer N, your task is to compute the sum of all the digits of N, and output e ...
- [ZJOI2019]麻将(DP+有限状态自动机)
首先只需要考虑每种牌出现的张数即可,然后判断一副牌是否能胡,可以DP一下,令f[i][j][k][0/1]表示到了第i位,用j次i-1,i,i+1和k次i,i+1,i+2,是否出现对子然后最大的面子数 ...
- 基础篇四:Ngnix安装
然后直接 yum install nginx 安装nginx
- win10//ubuntu安装tensorflow-gpu与kears,并用minist测试
WIn10 安装cuda 先安装VS,然后根据自己的版本安装CUDA. 安装完后,打开cmd命令行输入nvcc -V,检测是否安装成功 安装cuDDN 安装对应版本,解压后覆盖到CUDA的地址,默认为 ...
- [LC] 78. Subsets
Given a set of distinct integers, nums, return all possible subsets (the power set). Note: The solut ...
- C#函数的基础应用
C#函数的基础应用 函数之前的知识回顾 数据类型--变量常量--运算符表达式--语句(顺序,分支,循环)--数组--函数 程序里的函数:能完成一个相对独立功能的代码模块. 数学里的函数:高度抽象. 函 ...