[Algorithm] 53. Maximum Subarray
Given 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 you have figured out the O(n) solution, try coding another solution using the divide and conquer approach, which is more subtle.
/**
* @param {number[]} nums
* @return {number}
*/
var maxSubArray = function(nums) {
if (!nums.length) {
return null;
} let start = end = -1;
let crtDiff = maxDiff = Number.NEGATIVE_INFINITY; for (let i = 0; i < nums.length; i++) { crtDiff = Math.max(nums[i], crtDiff + nums[i])
maxDiff = Math.max(crtDiff, maxDiff);
} return maxDiff;
};
To locate the sub array:
var maxSubArray = function(nums) {
if (!nums.length) {
return null;
}
let start = end = -1;
let crtDiff = maxDiff = Number.NEGATIVE_INFINITY;
for (let i = 0; i < nums.length; i++) {
crtDiff = Math.max(nums[i], crtDiff + nums[i])
maxDiff = Math.max(crtDiff, maxDiff);
if (crtDiff === maxDiff) {
if (nums[i] === crtDiff) {
start = end = i;
}
if (maxDiff > nums[i] && start < 0) {
start = end = i;
} else if (maxDiff > nums[i] && start >= 0) {
end = i;
}
}
}
const sub = nums.slice(start, end + 1)
return maxDiff;
};
// [ 4, -1, 2, 1 ] sum 6
[Algorithm] 53. Maximum Subarray的更多相关文章
- [array] leetcode - 53. Maximum Subarray - Easy
leetcode - 53. Maximum Subarray - Easy descrition Find the contiguous subarray within an array (cont ...
- [Leetcode][Python]53: Maximum Subarray
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 53: Maximum Subarrayhttps://leetcode.co ...
- 41. leetcode 53. Maximum Subarray
53. Maximum Subarray Find the contiguous subarray within an array (containing at least one number) w ...
- 53. Maximum Subarray【leetcode】
53. Maximum Subarray[leetcode] Find the contiguous subarray within an array (containing at least one ...
- Leetcode#53.Maximum Subarray(最大子序和)
题目描述 给定一个序列(至少含有 1 个数),从该序列中寻找一个连续的子序列,使得子序列的和最大. 例如,给定序列 [-2,1,-3,4,-1,2,1,-5,4], 连续子序列 [4,-1,2,1] ...
- 小旭讲解 LeetCode 53. Maximum Subarray 动态规划 分治策略
原题 Given an integer array nums, find the contiguous subarray (containing at least one number) which ...
- LN : leetcode 53 Maximum Subarray
lc 53 Maximum Subarray 53 Maximum Subarray Find the contiguous subarray within an array (containing ...
- Leetcode之53. Maximum Subarray Easy
Leetcode 53 Maximum Subarray Easyhttps://leetcode.com/problems/maximum-subarray/Given an integer arr ...
- leetcode 53. Maximum Subarray 、152. Maximum Product Subarray
53. Maximum Subarray 之前的值小于0就不加了.dp[i]表示以i结尾当前的最大和,所以需要用一个变量保存最大值. 动态规划的方法: class Solution { public: ...
随机推荐
- base62与long的相互转换
public static class Converter { private static String keys = "0123456789abcdefghijklmnopqrstuvw ...
- Linux record
1.设置ubuntu密码刚安装好的ubuntu系统,没有root密码,需要用户去手动设置的. sudo passwd root 输入2次密码即可. 2. Linux下is not in the sud ...
- 读了两章的 How Tomcat Works
周一发现了一本书.How Tomcat Works 惯例先到豆瓣搜书评.结果书评出奇的好.然后下载了PDF.从简介中看,本书的每个章节都会不断的围绕怎么建造一个Tomcat讲解.我本人比较喜欢这种造轮 ...
- 论文阅读: Infrastructure-Based Calibration of a Multi-Camera Rig
Abstract 在线标定很重要. 但是目前的方法都计算量都很高. 我们的方案不需要标定板之类的东西. 我们的方案不需要假设相机有重合的FOV,也不需要任何的初始猜测. 当相机模组行驶穿过之前建过地图 ...
- Eureka的集群配置
1:步骤说明 2:修改映射配置 3:修改Eureka的.yml配置文件 以其中一个为例 4:在8001中同时注册 5:测试结果
- 新安装NODEJS之后配置
1配置阿里镜像服务器 npm config set registry https://registry.npm.taobao.org --global npm config set disturl h ...
- 以Integer类型传参值不变来理解Java值传参
最近在写代码的时候出了一个错误,由于对值引用理解的不深,将Integer传入方法中修改,以为传入后直接修改Integer中的值就不用写返回值接收了,虽然很快发现了问题,但还是来总结一下 首先是代码: ...
- JS 学习笔记
在JS中两个对象不能用“==” 或者“===” 来比较,如果硬是要比较的话,始终返回的是false var x = new String("Bill"); var y = new ...
- 一、hexo+github搭建个人博客的过程记录
前提: 1.新建一个github仓库 2.安装配置Node.js 3.安装配置Git 前提 步骤1.新建一个github仓库 打开github网站,(注册)登录账号,新建一个仓库; 注:==仓库名称要 ...
- 微信小程序练习笔记(更新中。。。)
微信小程序练习笔记 微信小程序的练习笔记,用来整理思路的,文档持续更新中... 案例一:实现行的删除和增加操作 test.js // 当我们在特定方法中创建对象或者定义变量给与初始值的时候,它是局部 ...