Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions maximum-subarray/yeonjukim164.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
class Solution {
public int maxSubArray(int[] nums) {
if (nums == null || nums.length == 0) {
return 0;
}
Comment on lines +3 to +5
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: 문제의 제약 조건으로 1 <= nums.length <= 10^5 명시되어 있으니 이 입력값 체크는 필요 없을 것 같습니다.


int currentSum = nums[0];
int maxSum = nums[0];

for (int i = 1; i < nums.length; i++) {
// 현재까지의 연속 합을 이어갈지, 새로 시작할지 결정
currentSum = Math.max(nums[i], currentSum + nums[i]);

// 전역 최대값 업데이트
maxSum = Math.max(maxSum, currentSum);
}

return maxSum;
}
}