Last active
December 15, 2015 17:19
-
-
Save wkschwartz/5295571 to your computer and use it in GitHub Desktop.
Java utility method for helping me find outliers for the Princeton/Coursera Algorithms Part 2 course assignment "WordNet."
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
/** | |
* Reflect a upper triangular matrix across its diagonal and return the row | |
* index whose sum is greatest, without modifying the input matrix. | |
* <p> | |
* The non-mutating reflection is accomplished by adding down column <em>i</em> | |
* from 0 to <em>i</em> - 1 and adding across row <em>i</em> from <em>i</em> to | |
* <code>a[i].length</code>. | |
* | |
* @param a An array of arrays of integers in row-major form. The arrays should | |
* but do not have to be equal lengths. | |
* @return The row index whose sum after reflection is greatest. | |
* @author William Schwartz | |
*/ | |
public int[] argMaxUpperTriangular(int[][] a) { | |
int sum, max, argmax; | |
max = 0; | |
for (int i = 0; i < a.length; i++) { | |
sum = 0; | |
for (int j = 0; j < a[i].length; j++) | |
sum += j < i ? a[j][i] : a[i][j]; | |
if (i == 0 || sum > max) { | |
min = sum; | |
argmax = i; | |
} | |
} | |
return argmax; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment