Skip to content

Commit c5efe09

Browse files
authored
Update 53-maximum-subarray.js
1 parent 27250dd commit c5efe09

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed

53-maximum-subarray.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,23 @@ const maxSubArray = function(nums) {
1414

1515
// another
1616

17+
/**
18+
* @param {number[]} nums
19+
* @return {number}
20+
*/
21+
const maxSubArray = function(nums) {
22+
const n = nums.length, dp = Array(n).fill(0)
23+
dp[0] = nums[0]
24+
let res = dp[0]
25+
for(let i = 1; i < n; i++) {
26+
dp[i] = Math.max(dp[i - 1], 0) + nums[i]
27+
res = Math.max(res, dp[i])
28+
}
29+
return res
30+
};
31+
32+
// another
33+
1734
/**
1835
* @param {number[]} nums
1936
* @return {number}

0 commit comments

Comments
 (0)