Created
July 11, 2016 15:58
-
-
Save tiagopereira17/ae6082d9d37b1ce2c520e049f58b60b2 to your computer and use it in GitHub Desktop.
Find a maximum sum of a compact subsequence of array elements.
This file contains hidden or 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 maxSliceSum(int[] A) { | |
if(A == null || A.length == 0) { | |
return 0; | |
} | |
int maxSum = Integer.MIN_VALUE; | |
int tempMaxSum = 0; | |
for(int i = 0; i < A.length; i++) { | |
tempMaxSum = Math.max(A[i], A[i] + tempMaxSum); | |
maxSum = Math.max(tempMaxSum, maxSum); | |
} | |
return maxSum; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment