There is a m x n matrix given each with integer elements. The problem says that we have to place two rooks on that matrix, such that they don't attack each other and the sum of the elements on which the rooks are placed is maximum.
Example: Let's say matrix is
2 5
1 3
Then non-attacking rooks can be placed only on 2,3 or 1,5 element positions. But the maximum sum is found in case of 1,5, so the function should return 1 + 5 = 6.
I thought that we could go through all the pairs of the array one by one and then return the maximum sum that we found, but I can't seem to find a better or efficient approach for this. My solution would be O(m * m * n * n)
in terms of complexity.
What would be a better approach? I would appreciate any help.
HIGHER ROW
, sum that top value with the top value for the row,if the COLUMN for that cell is != the COLUMN for the set Pretend ROOK, ELSE, use the SECOND value from that ROW
. Remember the row of the pretend rook and the column with the highest sum. – Hovis