grid (m rows, n columns) are labeled with integers, their values, which may be negative.
Place three rooks so that no two of them share a row or a column (so none attacks another), and return the maximum total value of the occupied cells.
The board can be as large as 500 × 500, so trying every triple of cells will not fit in the time limit.grid=[[1, 8, 3, 2], [7, 9, 0, 1], [4, 6, 5, 2]] → 20
The most valuable cell, 9, blocks both the 8 and the 7. Taking 8 + 7 + 5 = 20 on (0, 1), (1, 0), (2, 2) is better.
grid=[[3, 3, 3], [3, 3, 3], [3, 3, 3]] → 9
Any placement with no shared row or column works: 3 + 3 + 3 = 9.
grid=[[0, -4, 10], [-4, 0, 10], [10, -4, 0]] → 20
grid=[[1, 8, 3, 2], [7, 9, 0, 1], [4, 6, 5, 2]]
20
Run checks the first 3 tests in your browser. Submit checks all 84 tests on the server, hidden ones included.