Skip to content

Commit 36a3ba1

Browse files
committed
maximum-subarray
1 parent f5eee34 commit 36a3ba1

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed

maximum-subarray/jun0811.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/**
2+
* @param {number[]} nums
3+
* @return {number}
4+
*/
5+
var maxSubArray = function (nums) {
6+
const dp = [nums[0]];
7+
for (let i = 1; i < nums.length; i++) {
8+
const cur = nums[i];
9+
if (dp[i - 1] < 0) dp[i] = cur;
10+
else {
11+
dp[i] = dp[i - 1] + cur;
12+
}
13+
}
14+
return Math.max(...dp);
15+
};

0 commit comments

Comments
 (0)