Skip to content

Commit 6d4765e

Browse files
committed
Maximum Subarray
1 parent 69030c0 commit 6d4765e

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed

maximum-subarray/TonyKim9401.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// TC: O(n)
2+
// visit all elements once for each
3+
// SC: O(1)
4+
// constant space occupation
5+
class Solution {
6+
public int maxSubArray(int[] nums) {
7+
int total = 0;
8+
int output = nums[0];
9+
10+
for (int num : nums) {
11+
if (total < 0) total = 0;
12+
total += num;
13+
output = total > output ? total : output;
14+
}
15+
return output;
16+
}
17+
}

0 commit comments

Comments
 (0)