Skip to content

Commit a6b54a4

Browse files
committed
maximum subarray
1 parent 2dca348 commit a6b54a4

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed

maximum-subarray/eunhwa99.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// 시간 복잡도: DP -> O(N)
2+
// 공간 복잡도: nums 배열 크기 - O(N)
3+
4+
class Solution {
5+
public int maxSubArray(int[] nums) {
6+
int currentSum = nums[0];
7+
int maxSum = currentSum;
8+
for (int i = 1; i < nums.length; ++i) {
9+
currentSum = Math.max(currentSum + nums[i], nums[i]);
10+
maxSum = Math.max(maxSum, currentSum);
11+
}
12+
13+
return maxSum;
14+
}
15+
}

0 commit comments

Comments
 (0)