For nxn matrix,
[0,0] can be swapped with [0,n-1], [n-1,0], [n-1,n-1],
[0,1] can be swapped with [0,n-1 -1], [n-1, 1], [n-1,n-1 -1],
and so on.
It is possible to swap any item without changing the other elements in upper left quadrant.
So, maxij = max(a[i,j], a[i,n-1-j], a[n-1-i,j], a[n-1-i,n-1-j])
(m): if m == null || m.length() == 0 || m[0].length() == 0: throw if m.length() &1 != 0 || m[0].length() &1 != 0: throw "matrix size is not even" if m.length() != m[0].length(): throw "matrix size is not equal" n = m.length() maxsum = 0 for i = 0, i < n/2, i++: for i = 0, i < n/2, i++: maxij = max(a[i,j], a[i,n-1-j], a[n-1-i,j], a[n-1-i,n-1-j]) maxsum += maxij return maxsum[Hat tip to HR,JK]
No comments:
Post a Comment