-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
7768562
commit 0214a36
Showing
3 changed files
with
131 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
--- | ||
title: 53.最大子数组和 | ||
date: 2024-03-24 | ||
lang: 'zh-CN' | ||
sidebar: 'auto' | ||
categories: | ||
- LeeCode | ||
tags: | ||
location: HangZhou | ||
--- | ||
|
||
# Heading | ||
|
||
[[toc]] | ||
|
||
[53.最大子数组和](https://leetcode.cn/problems/maximum-subarray/description/) | ||
|
||
Tags: algorithms bloomberg linkedin microsoft array divide-and-conquer dynamic-programming | ||
|
||
Langs: c cpp csharp dart elixir erlang golang java javascript kotlin php python python3 racket ruby rust scala swift typescript | ||
|
||
- algorithms | ||
- Medium (55.22%) | ||
- Likes: 6594 | ||
- Dislikes: - | ||
- Total Accepted: 1.7M | ||
- Total Submissions: 3M | ||
- Testcase Example: '[-2,1,-3,4,-1,2,1,-5,4]' | ||
|
||
<p>给你一个整数数组 <code>nums</code> ,请你找出一个具有最大和的连续子数组(子数组最少包含一个元素),返回其最大和。</p> | ||
|
||
<p><strong><span data-keyword="subarray-nonempty">子数组 </span></strong>是数组中的一个连续部分。</p> | ||
|
||
<p> </p> | ||
|
||
<p><strong>示例 1:</strong></p> | ||
|
||
<pre> | ||
<strong>输入:</strong>nums = [-2,1,-3,4,-1,2,1,-5,4] | ||
<strong>输出:</strong>6 | ||
<strong>解释:</strong>连续子数组 [4,-1,2,1] 的和最大,为 6 。 | ||
</pre> | ||
|
||
<p><strong>示例 2:</strong></p> | ||
|
||
<pre> | ||
<strong>输入:</strong>nums = [1] | ||
<strong>输出:</strong>1 | ||
</pre> | ||
|
||
<p><strong>示例 3:</strong></p> | ||
|
||
<pre> | ||
<strong>输入:</strong>nums = [5,4,-1,7,8] | ||
<strong>输出:</strong>23 | ||
</pre> | ||
|
||
<p> </p> | ||
|
||
<p><strong>提示:</strong></p> | ||
|
||
<ul> | ||
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li> | ||
<li><code>-10<sup>4</sup> <= nums[i] <= 10<sup>4</sup></code></li> | ||
</ul> | ||
|
||
<p> </p> | ||
|
||
<p><strong>进阶:</strong>如果你已经实现复杂度为 <code>O(n)</code> 的解法,尝试使用更为精妙的 <strong>分治法</strong> 求解。</p> | ||
|
||
<<< @/src/LeeCode/53.最大子数组和.js | ||
<<< @/src/LeeCode/53.最大子数组和1.js |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
/* | ||
* @lc app=leetcode.cn id=53 lang=javascript | ||
* | ||
* [53] 最大子数组和 | ||
* 动态规划 | ||
*/ | ||
|
||
// @lc code=start | ||
/** | ||
* @param {number[]} nums | ||
* @return {number} | ||
*/ | ||
var maxSubArray = function (nums) { | ||
if (nums.length < 1) { | ||
return 0 | ||
} | ||
|
||
// f(n) = max(f(n-1)+nums(n),nums(n)); | ||
let max = nums[0] | ||
const dp = [nums[0]] | ||
for (let i = 1; i < nums.length; i++) { | ||
if (dp[i - 1] + nums[i] > nums[i]) { | ||
dp[i] = dp[i - 1] + nums[i] | ||
} else { | ||
dp[i] = nums[i] | ||
} | ||
if (dp[i] > max) { | ||
max = dp[i] | ||
} | ||
} | ||
return max | ||
} | ||
// @lc code=end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
/* | ||
* @lc app=leetcode.cn id=53 lang=javascript | ||
* | ||
* [53] 最大子数组和 | ||
* 贪心 | ||
*/ | ||
|
||
// @lc code=start | ||
/** | ||
* @param {number[]} nums | ||
* @return {number} | ||
*/ | ||
var maxSubArray = function (nums) { | ||
if (nums.length < 1) { | ||
return 0 | ||
} | ||
|
||
let maxSum = nums[0] | ||
let currentSum = nums[0] | ||
for (let i = 1; i < nums.length; i++) { | ||
currentSum = Math.max(currentSum + nums[i], nums[i]) | ||
maxSum = Math.max(currentSum, maxSum) | ||
} | ||
return maxSum | ||
} | ||
// @lc code=end |