Skip to content

Commit 666e72c

Browse files
week12 mission maximum-subarray
1 parent b2508f2 commit 666e72c

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
- 문제: https://leetcode.com/problems/maximum-subarray/
2+
- 풀이: https://algorithm.jonghoonpark.com/2024/05/07/leetcode-53
3+
4+
```java
5+
class Solution {
6+
public int maxSubArray(int[] nums) {
7+
int maxSum = Integer.MIN_VALUE;
8+
int currentSum = 0;
9+
10+
for (int i = 0; i < nums.length; i++) {
11+
currentSum += nums[i];
12+
maxSum = Math.max(maxSum, currentSum);
13+
currentSum = Math.max(currentSum, 0);
14+
}
15+
16+
return maxSum;
17+
}
18+
}
19+
```
20+
21+
- currentSum이 maxSum보다 클 경우 maxSum을 갱신한다.
22+
- currentSum은 음수일 경우 (0보다 작을 경우) 0으로 초기화 한다.
23+
24+
### TC, SC
25+
26+
시간 복잡도는 O(n), 공간 복잡도는 O(1) 이다.

0 commit comments

Comments
 (0)