We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent b2508f2 commit 666e72cCopy full SHA for 666e72c
maximum-subarray/dev-jonghoonpark.md
@@ -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