Skip to content

Commit

Permalink
feat(leecode): 53.最大子数组和
Browse files Browse the repository at this point in the history
  • Loading branch information
z979054461 committed Mar 24, 2024
1 parent 7768562 commit 0214a36
Show file tree
Hide file tree
Showing 3 changed files with 131 additions and 0 deletions.
72 changes: 72 additions & 0 deletions docs/10.LeeCode/53.最大子数组和.md
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>&nbsp;</p>

<p><strong>示例 1:</strong></p>

<pre>
<strong>输入:</strong>nums = [-2,1,-3,4,-1,2,1,-5,4]
<strong>输出:</strong>6
<strong>解释:</strong>连续子数组&nbsp;[4,-1,2,1] 的和最大,为&nbsp;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>&nbsp;</p>

<p><strong>提示:</strong></p>

<ul>
<li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li>
<li><code>-10<sup>4</sup> &lt;= nums[i] &lt;= 10<sup>4</sup></code></li>
</ul>

<p>&nbsp;</p>

<p><strong>进阶:</strong>如果你已经实现复杂度为 <code>O(n)</code> 的解法,尝试使用更为精妙的 <strong>分治法</strong> 求解。</p>

<<< @/src/LeeCode/53.最大子数组和.js
<<< @/src/LeeCode/53.最大子数组和1.js
33 changes: 33 additions & 0 deletions src/LeeCode/53.最大子数组和.js
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
26 changes: 26 additions & 0 deletions src/LeeCode/53.最大子数组和1.js
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

0 comments on commit 0214a36

Please sign in to comment.