[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: ...
随机推荐
- mysql Duplicate entry '9223372036854775807' for key 'PRIMARY'
mysql插入数据报错提示: ERROR 1062(23000) Duplicate entry '9223372036854775807' for key 'PRIMARY' 发现问题果断 直接 ...
- Javascript笔记:作用域和执行上下文
一.作用域 Javascript的作用域规则是在编译阶段确定的,有声明时的位置决定. JS中有全局作用域,函数作用域,块级作用域(ES6引入). 1. 全局作用域 在整个程序生命周期内都是有效的,在任 ...
- [数据库] SQL 语法之基础篇
一.什么是 SQL ? SQL 是 Structured Query Language(结构化查询语言)的缩写,是一种专门用来与数据库沟通的语言.与其他语言(如英语或 C.C++.Java 这样的编程 ...
- C++ 函数重载和参数的缺省值
一.函数重载 1.1 重载的起源 自然语言中,一个词可以有许多不同的含义,即该词被重载了.人们可以通过上下文来判断该词到底是哪种含义."词的重载"可以使语言更加简练.例如" ...
- Python处理数据集-2
原数据集的数据格式: 每行为:(test_User, test_Item) negativeItem1 negativeItem2 negativeItem3 …… negativeItem99 即每 ...
- SQL -------- 简单的增删改查
sql 结构化查询语言,一种ansi 的标准计算机语言,为了访问数据库 可以做什么:可以对数据库 和表进行创建于删除, 对表里面的数据进行增删改查. 也可以创建存储过程和视图,对表设置权限 RDBM ...
- 微信小程序起步
微信小程序 文档 微信小程序开发文档 本质 so微信小程序到底是什么?是原生的app还是H5应用? 简单来说,小程序是一种应用,运行的环境是微信(App)进程中,使用了部分的H5技术 目录介绍 app ...
- MAC安装Node.js
官网下载Node.js Node.js v10.16.3 to /usr/local/bin/node • npm v6.9.0 to /usr/local/bin/npm Make sure tha ...
- redis 实战操作RDB和AOF快照持久化
前言:redis是我们常用的缓存方式,今天就来介绍下两种持久化的方式吧,先科普概念,再实战操作 一.RDB Redis将某一时刻的快照(备份的数据库数据)保存成一种称为RDB格式的文件中,这种格式是经 ...
- iviewUI 前端静态页面实现增删改查分页
完整代码部分 (仅供参考哈): <template> <div> <label prop="name"> 姓名: </label> ...