Skip to content

Commit 6b89398

Browse files
author
jinvicky
committed
max subarray solution
1 parent f273007 commit 6b89398

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

maximum-subarray/jinvicky.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
class Solution {
2+
public int maxSubArray(int[] nums) {
3+
int res = nums[0];
4+
int total = 0;
5+
6+
for (int n : nums) {
7+
if (total < 0) {
8+
total = 0;
9+
}
10+
total += n;
11+
res = Math.max(total, res);
12+
13+
}
14+
return res;
15+
}
16+
}

0 commit comments

Comments
 (0)