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 e92b934 commit d3c9c58Copy full SHA for d3c9c58
1 file changed
maximum-subarray/dohyeon2.java
@@ -0,0 +1,17 @@
1
+class Solution {
2
+ // TC : O(n)
3
+ // SC : O(1)
4
+ public int maxSubArray(int[] nums) {
5
+ int max = nums[0];
6
+ int sum = 0;
7
+
8
+ for (int i = 0; i < nums.length; i++) {
9
+ // 부분합보다 현재 숫자가 더 크면 갱신한다.
10
+ // 현재 숫자 단독이 더 큰 경우 이전 합이 의미가 없기 때문에 그리디하게 처리 가능
11
+ sum = Math.max(nums[i], nums[i] + sum);
12
+ max = Math.max(sum, max);
13
+ }
14
15
+ return max;
16
17
+}
0 commit comments