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的更多相关文章

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

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

  2. [Leetcode][Python]53: Maximum Subarray

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 53: Maximum Subarrayhttps://leetcode.co ...

  3. 41. leetcode 53. Maximum Subarray

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

  4. 53. Maximum Subarray【leetcode】

    53. Maximum Subarray[leetcode] Find the contiguous subarray within an array (containing at least one ...

  5. Leetcode#53.Maximum Subarray(最大子序和)

    题目描述 给定一个序列(至少含有 1 个数),从该序列中寻找一个连续的子序列,使得子序列的和最大. 例如,给定序列 [-2,1,-3,4,-1,2,1,-5,4], 连续子序列 [4,-1,2,1] ...

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

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

  7. LN : leetcode 53 Maximum Subarray

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

  8. Leetcode之53. Maximum Subarray Easy

    Leetcode 53 Maximum Subarray Easyhttps://leetcode.com/problems/maximum-subarray/Given an integer arr ...

  9. leetcode 53. Maximum Subarray 、152. Maximum Product Subarray

    53. Maximum Subarray 之前的值小于0就不加了.dp[i]表示以i结尾当前的最大和,所以需要用一个变量保存最大值. 动态规划的方法: class Solution { public: ...

随机推荐

  1. Servlet的Listener介绍

    当Web应用在Web容器中运行时,Web应用内部会不断地发生各种事件:如Web应用被启动.Web应用被停止.用户session开始.用户session结束等.通常这些Web操作对开发者是透明的.但Se ...

  2. 动手学深度学习4-线性回归的pytorch简洁实现

    导入同样导入之前的包或者模块 生成数据集 通过pytorch读取数据 定义模型 初始化模型 定义损失函数 定义优化算法 训练模型 小结 本节利用pytorch中的模块,生成一个更加简洁的代码来实现同样 ...

  3. 【转】PyQt弹出式对话框的常用方法及标准按钮类型

    pyQt之弹出式对话框(QMessageBox)的常用方法及标准按钮类型 一.控件说明 QMessageBox是一种通用的弹出式对话框,用于显示消息,允许用户通过单击不同的标准按钮对消息进行反馈,且每 ...

  4. Python 脚本如何执行另一个脚本

    关于Python 脚本如何执行另一个脚本,可以使用os.system()来实现 os.system()的参数: 执行的命令 +执行的内容 举例说明: (1)显示当前文件夹下的全部目录和文件夹 os.s ...

  5. axios的各种传参方式

    axios的各种传参方式 1. params方式 axios({ url: '/users', method: 'get', params: { id: '11111', name: '22222' ...

  6. Vue.js 源码分析(十二) 基础篇 组件详解

    组件是可复用的Vue实例,一个组件本质上是一个拥有预定义选项的一个Vue实例,组件和组件之间通过一些属性进行联系. 组件有两种注册方式,分别是全局注册和局部注册,前者通过Vue.component() ...

  7. Prometheus 运维监控

    Prometheus 运维监控 1.Prometheus 介绍详解 2.Prometheus 安装部署 3.Prometheus 配置文件详解 4.Prometheus PromSQL 常用资源 5. ...

  8. 微信小程序报thirdScriptError Cannot read property 'setData' of undefined

    用onLoad: function (options) {} 加载页面缓存的数据的时候报错   修改为          

  9. 基于串口的SD_card系统

    概述 基于串口的SD_card系统1, 扫描文件:2, 新建文件:3, 删除文件:4, 写入文件:5, 读取文件. 整个文件系统的串口通信方式都是ASC通信方式. 文件系统分为简单实用方式和专业使用方 ...

  10. 4种引用与垃圾回收 :StrongReference, SoftReference, WeakReference , PhantomReference