[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: ...
随机推荐
- 深入理解C语言 - 指针详解
一.什么是指针 C语言里,变量存放在内存中,而内存其实就是一组有序字节组成的数组,每个字节有唯一的内存地址.CPU 通过内存寻址对存储在内存中的某个指定数据对象的地址进行定位.这里,数据对象是指存储在 ...
- 论文阅读: Direct Monocular Odometry Using Points and Lines
Direct Monocular Odometry Using Points and Lines Abstract 大多数VO都用点: 特征匹配 / 基于像素intensity的直接法关联. 我们做了 ...
- HTTP之Web服务器是如何进行工作的!
Web服务器是如何进行工作的 ====================文章摘自<HTTP权威指南>====================== 1. 建立连接—接收一个客户端的连接,或者 ...
- antd Table 可伸缩列没有效果
把antd 中的Table可伸缩示例代码,下载到自己的代码中发现,鼠标放到表格的边框上,并没有出现可伸缩鼠标样式,最后的解决方法是在css样式中添加下面的样式,就解决问题了. .react-resiz ...
- IDEA rider 管道模式 经典模式(2)
1.这里设置为Classic,并打开applicationhost.config将对应应用的 Clr4IntegratedAppPool全部替换为 Clr4ClassicAppPool 2.Confi ...
- sentry之一:sentry安装
Sentry 是一个开源的实时错误追踪系统,可以帮助开发者实时监控并修复异常问题.它主要专注于持续集成.提高效率并且提升用户体验.Sentry 分为服务端和客户端 SDK,前者可以直接使用它家提供的在 ...
- 解决 警告: [SetPropertiesRule]{Server/Service/Engine/Host/Context} Setting property 'source' to 'org.eclipse.jst.jee.server:reyo' did not find a matching property.
解决办法是:关闭tomcat,双击eclipse下tomcat服务器,在出来的Tomcat server at localhost页面中找到server options选项,选中其中的选项”Publi ...
- WPF 精修篇 管理资源字典
原文:WPF 精修篇 管理资源字典 样式太多 每个界面可能需要全局的样式 有没有肯能 WPF 中的样式 像Asp.net中 的CSS一样管理那 有的 有资源字典 BurshDictionary &l ...
- Visual Studio 2019 正式版今日发布 key
Visual Studio 2019 EnterpriseBF8Y8-GN2QH-T84XB-QVY3B-RC4DFVisual Studio 2019 ProfessionalNYWVH-HT4X ...
- 基于.net core 3 和 Orleans 3 的 开发框架:Phenix Framework 7
Phenix Framework 7 for .net core 3 + Orleans 3 发布地址:https://github.com/phenixiii/Phenix.NET7 2019052 ...