Created
July 28, 2015 16:00
-
-
Save oskar-j/b0ec43655d09c0d7affd to your computer and use it in GitHub Desktop.
Find maximum sum in slices (subsequences) of an array in Java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class MaxSliceSum { | |
public int solution(int[] A) { | |
int ans = A[0], sum = 0; | |
for (int i = 0; i < A.length; i++) { | |
if (sum > 0) { | |
sum += A[i]; | |
} else { | |
sum = A[i]; | |
} | |
ans = Math.max(ans, sum); | |
} | |
return ans; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment