Skip to content

Commit 778ae3e

Browse files
committed
feat: 53. Maximum Subarray
1 parent c787f33 commit 778ae3e

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

maximum-subarray/gwbaik9717.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// Time complexity: O(n)
2+
// Space complexity: O(n)
3+
4+
/**
5+
* @param {number[]} nums
6+
* @return {number}
7+
*/
8+
var maxSubArray = function (nums) {
9+
const n = nums.length;
10+
const dp = Array.from({ length: n + 1 }, () => Number.MIN_SAFE_INTEGER);
11+
dp[0] = 0;
12+
13+
for (let i = 1; i <= n; i++) {
14+
dp[i] = Math.max(dp[i - 1] + nums[i - 1], nums[i - 1]);
15+
}
16+
17+
return Math.max(...dp.slice(1));
18+
};

0 commit comments

Comments
 (0)