-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMaximumSubarraySum.java
More file actions
31 lines (25 loc) · 957 Bytes
/
Copy pathMaximumSubarraySum.java
File metadata and controls
31 lines (25 loc) · 957 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
package kyu5;
public class MaximumSubarraySum {
//5
public static int sequence(int[] arr) {
if (arr == null) {
throw new IllegalArgumentException("array must not be null");
}
long best = 0L;
long current = 0L;
for (int value : arr) {
current = Math.max(0L, current + value);
best = Math.max(best, current);
}
return Math.toIntExact(best);
}
/**
* The maximum sum subarray problem consists in finding the maximum sum of a contiguous subsequence
* in an array or list of integers:
* <p>
* Easy case is when the list is made up of only positive numbers and the maximum sum is the sum of the whole array.
* If the list is made up of only negative numbers, return 0 instead.
* <p>
* Empty list is considered to have zero greatest sum. Note that the empty list or array is also a valid sublist/subarray.
*/
}