Created
February 3, 2014 10:00
-
-
Save the-fejw/8781249 to your computer and use it in GitHub Desktop.
CLRS 4.1.2 Brute Force
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 static int[] bruteForce(int[] A, int low, int high) { | |
int[] result = new int[3]; //result[0]: max-left, result[1]: max-right, result[2]: max-sum | |
int sum = 0; | |
int maxSum = Integer.MIN_VALUE; | |
for (int i = low; i <= high; i++) { | |
for (int j = i; j <= high; j++) { | |
sum += A[j]; | |
if (sum > maxSum) { | |
maxSum = sum; | |
result[0] = i; | |
result[1] = j; | |
} | |
} | |
sum = 0; | |
} | |
result[2] = maxSum; | |
return result; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment