[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: ...
随机推荐
- Netty FixedChannelPool
如今越来越多的应用采用Netty作为服务端高性能异步通讯框架,对于客户端而言,大部分需求只需和服务端建立一条链接收发消息.但如果客户端需要和服务端建立多条链接的例子就比较少了. 最简单的实现就是一个f ...
- Django-查询优化
表数据: from django.db import models class Province(models.Model): name = models.CharField(max_length=1 ...
- layer弹出框,zIndex不断增加的问题
针对layer弹出框每次进行弹出操作时z-index不断加1的问题,手动设置过zIndex值不管用,每次关闭时清空layer对象也不管用. 解决办法: 修改layer.js,,将红框代码改为绿框代码, ...
- Windows 配置网络文件夹映射
mklink /D D:\temp\pythonmxds2 \\192.168.190.186\bigdata\kaoyanmxds
- 如何解决aws解绑银行卡问题?
首先先来说明一下我自己的情况? 一年的免费使用 前提:没有开启任何的实例服务 先贴一条官方的解释 关于我小白一个.学校课程要求使用aws,注册之后在网络上看到一堆人踩坑,aws的扣费就是个坑! 预授权 ...
- Redis(一) redis安装、启停
Redis是开源的内存数据存储,常被用作为内存数据库.缓存.全局队列.计数器等等. Redis安装 Redis分为多种模式:单机模式.高可用模式.集群模式.这篇中主要简介单机版的安装方式. 源码构建式 ...
- 【01】Python:故事从这里开始
写在前面的话 最近在 Github 上面看到一个 100 天学习 Python 项目: https://github.com/jackfrued/Python-100-Days 于是便想着抽时间将自己 ...
- C# WebBrowser控件 下载文件不弹下载提示框的办法
参考链接 https://stackoverflow.com/questions/6773866/download-file-and-automatically-save-it-to-folder p ...
- TCP/UDP协议(二)
面试问题:Tcp/Udp协议是什么,各有什么异同点,各自的使用场景? Tcp协议(传输控制协议) tcp是面向连接的协议,在收发数据之前,必须与对方建立可靠的连接: 三次握手:简单形象通俗描述: 主机 ...
- 排序算法Java代码实现(四)—— 归并排序
本篇内容: 归并排序 归并排序 算法思想: 将两个或两个以上的有序表合并成一个新的有序表, 即把待排序序列分成若干个子序列,每个子序列是有序的,然后在把有序子序列合并为整体有序序列. 此算法分为两步: ...