-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolution.java
More file actions
43 lines (36 loc) · 988 Bytes
/
Solution.java
File metadata and controls
43 lines (36 loc) · 988 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
package org.example.problems.maximum_subarray;
import org.example.problems.SolutionInterface;
public class Solution implements SolutionInterface {
@Override
public String getName() {
return "Maximum Subarray";
}
@Override
public String getUrl() {
return "https://leetcode.com/problems/maximum-subarray/";
}
public int maxSubArray(int[] nums) {
int maxSum = Integer.MIN_VALUE;
int sum = 0;
int rightBorder = 0;
for (int i = 0; i < nums.length; i++) {
int num = nums[i];
sum += num;
if (num > sum) {
sum = num;
}
if (sum > maxSum) {
maxSum = sum;
rightBorder = i;
}
}
sum = 0;
for (int i = rightBorder; i >= 0; i--) {
sum += nums[i];
if (sum > maxSum) {
maxSum = sum;
}
}
return maxSum;
}
}