Skip to content

Commit 9643613

Browse files
committed
🔥 Maximum Subarray
1 parent 2aead21 commit 9643613

File tree

2 files changed

+20
-6
lines changed

2 files changed

+20
-6
lines changed

README.md

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,10 @@ Solutions of LeetCode Blind 75 Problems in JavaScript
44

55
:goal_net: [Curated List of Top 75 LeetCode Questions](https://www.teamblind.com/post/New-Year-Gift---Curated-List-of-Top-75-LeetCode-Questions-to-Save-Your-Time-OaM1orEU)
66

7-
| Problem | Difficulty | Tags | LeetCode |
8-
| ----------------------------------------------------------------------- | --------------------------------------------------------- | --------------------------------------- | ------------------------------------------------------------------------ |
9-
| [Two Sum](./two-sum.js) | <img src="https://img.shields.io/badge/-Easy-green" /> | `Array`, `Hash Table` | [:link:](https://leetcode.com/problems/two-sum/) |
10-
| [Best Time to Buy and Sell Stock](./best-time-to-buy-and-sell-stock.js) | <img src="https://img.shields.io/badge/-Easy-green" /> | `Array`, `Dynamic Programming` | [:link:](https://leetcode.com/problems/best-time-to-buy-and-sell-stock/) |
11-
| [Contains Duplicate](./contains-duplicate.js) | <img src="https://img.shields.io/badge/-Easy-green" /> | `Array`, `Set`, `Sorting`, `Hash Table` | [:link:](https://leetcode.com/problems/contains-duplicate/) |
12-
| [Product of Array Except Self](./product-of-array-except-self.js) | <img src="https://img.shields.io/badge/-Medium-orange" /> | `Array` | [:link:](https://leetcode.com/problems/product-of-array-except-self/) |
7+
| Problem | Difficulty | Tags | LeetCode |
8+
| ----------------------------------------------------------------------- | --------------------------------------------------------- | -------------------------------------------------- | ------------------------------------------------------------------------ |
9+
| [Two Sum](./two-sum.js) | <img src="https://img.shields.io/badge/-Easy-green" /> | `Array`, `Hash Table` | [:link:](https://leetcode.com/problems/two-sum/) |
10+
| [Best Time to Buy and Sell Stock](./best-time-to-buy-and-sell-stock.js) | <img src="https://img.shields.io/badge/-Easy-green" /> | `Array`, `Dynamic Programming` | [:link:](https://leetcode.com/problems/best-time-to-buy-and-sell-stock/) |
11+
| [Contains Duplicate](./contains-duplicate.js) | <img src="https://img.shields.io/badge/-Easy-green" /> | `Array`, `Set`, `Sorting`, `Hash Table` | [:link:](https://leetcode.com/problems/contains-duplicate/) |
12+
| [Product of Array Except Self](./product-of-array-except-self.js) | <img src="https://img.shields.io/badge/-Medium-orange" /> | `Array` | [:link:](https://leetcode.com/problems/product-of-array-except-self/) |
13+
| [Maximum Subarray](./maximum-subarray.js) | <img src="https://img.shields.io/badge/-Easy-green" /> | `Array`, `Dynamic Programming`, `Divide & Conquer` | [:link:](https://leetcode.com/problems/maximum-subarray/) |

maximum-subarray.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
const maxSubArray = (nums) => {
2+
let max = nums[0],
3+
current = 0;
4+
5+
for (let num of nums) {
6+
if (current < 0) current = 0;
7+
current += num;
8+
9+
max = Math.max(max, current);
10+
}
11+
12+
return max;
13+
};

0 commit comments

Comments
 (0)