Last active
August 29, 2015 14:07
-
-
Save alexandervasyuk/a28f48a44177e0229cb4 to your computer and use it in GitHub Desktop.
largestContinuousSum
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 static LinkedList<Integer> largestContinuousSum(int[] array) { | |
LinkedList<Integer> current_path, max_path; | |
int current_sum, max_sum; | |
max_path = current_path = new LinkedList<Integer>(); | |
current_sum = max_sum = Integer.MIN_VALUE; | |
for (int i : array) { | |
if (current_sum < 0 && current_sum < i) { | |
current_path = new LinkedList<Integer>(); | |
current_path.add(i); | |
current_sum = i; | |
} else if (current_sum + i >= current_sum) { | |
current_path.add(i); | |
current_sum += i; | |
} else { | |
if (max_sum < current_sum) { | |
max_path = current_path; | |
max_sum = current_sum; | |
current_path = new LinkedList<Integer>(); | |
current_sum = 0; | |
} | |
} | |
} | |
if (current_sum > max_sum) { | |
max_path = current_path; | |
} | |
return max_path; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment