Skip to content

Commit

Permalink
solve: maximum subarray
Browse files Browse the repository at this point in the history
  • Loading branch information
wogha95 committed Oct 9, 2024
1 parent 1657c59 commit 06b49b9
Showing 1 changed file with 24 additions and 0 deletions.
24 changes: 24 additions & 0 deletions maximum-subarray/wogha95.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/**
* 각 num을 누적된 result값에 num을 더한값과 비교해서 큰 값을 result로 유지해간다.
* 그리고 최대 누적값을 구하기 위해 매 result를 구할때마다 최대 result를 갱신한다.
*
* TC: O(N)
* SC: O(1)
*/

/**
* @param {number[]} nums
* @return {number}
*/
var maxSubArray = function (nums) {
let maxResult = nums[0];
let result = nums[0];

for (let index = 1; index < nums.length; index++) {
const num = nums[index];
result = Math.max(result + num, num);
maxResult = Math.max(maxResult, result);
}

return maxResult;
};

0 comments on commit 06b49b9

Please sign in to comment.