Skip to content

Commit 3d3d782

Browse files
authored
Merge pull request DaleStudy#2283 from yeonjukim164/main
[yeonjukim164] WEEK 10 solution
2 parents 8f248e1 + 7783908 commit 3d3d782

1 file changed

Lines changed: 20 additions & 0 deletions

File tree

maximum-subarray/yeonjukim164.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
class Solution {
2+
public int maxSubArray(int[] nums) {
3+
if (nums == null || nums.length == 0) {
4+
return 0;
5+
}
6+
7+
int currentSum = nums[0];
8+
int maxSum = nums[0];
9+
10+
for (int i = 1; i < nums.length; i++) {
11+
// 현재까지의 연속 합을 이어갈지, 새로 시작할지 결정
12+
currentSum = Math.max(nums[i], currentSum + nums[i]);
13+
14+
// 전역 최대값 업데이트
15+
maxSum = Math.max(maxSum, currentSum);
16+
}
17+
18+
return maxSum;
19+
}
20+
}

0 commit comments

Comments
 (0)