Input: board =[[-3,1,1,1],[-3,1,-3,1],[-3,2,1,1]]Output: 4Explanation:
We can place the rooks in the cells `(0, 2)`,`(1, 3)`, and `(2, 1)`for a sum of `1 + 1 + 2 = 4`.
Input: board =[[1,2,3],[4,5,6],[7,8,9]]Output: 15Explanation:
We can place the rooks in the cells `(0, 0)`,`(1, 1)`, and `(2, 2)`for a sum of `1 + 5 + 9 = 15`.
Input: board =[[1,1,1],[1,1,1],[1,1,1]]Output: 3Explanation:
We can place the rooks in the cells `(0, 2)`,`(1, 1)`, and `(2, 0)`for a sum of `1 + 1 + 1 = 3`.
To maximize the sum, we need to place three rooks on the board such that no two share a row or column. This is equivalent to choosing three distinct rows and three distinct columns, and summing the values at those intersections. Enumerating all such combinations gives the optimal answer.
classSolution {
public:int maxValueSum(vector<vector<int>>& board) {
int m = board.size(), n = board[0].size(), ans = INT_MIN;
vector<int> rows(m), cols(n);
iota(rows.begin(), rows.end(), 0);
iota(cols.begin(), cols.end(), 0);
for (int r1 =0; r1 < m; ++r1)
for (int r2 = r1+1; r2 < m; ++r2)
for (int r3 = r2+1; r3 < m; ++r3)
for (int c1 =0; c1 < n; ++c1)
for (int c2 = c1+1; c2 < n; ++c2)
for (int c3 = c2+1; c3 < n; ++c3) {
vector<int> r = {r1, r2, r3}, c = {c1, c2, c3};
sort(c.begin(), c.end());
do {
int sum =0;
for (int i =0; i <3; ++i) sum += board[r[i]][c[i]];
ans = max(ans, sum);
} while (next_permutation(c.begin(), c.end()));
}
return ans;
}
};
classSolution {
publicintmaxValueSum(int[][] board) {
int m = board.length, n = board[0].length, ans = Integer.MIN_VALUE;
for (int r1 = 0; r1 < m; ++r1)
for (int r2 = r1+1; r2 < m; ++r2)
for (int r3 = r2+1; r3 < m; ++r3)
for (int c1 = 0; c1 < n; ++c1)
for (int c2 = c1+1; c2 < n; ++c2)
for (int c3 = c2+1; c3 < n; ++c3) {
int[] r = {r1, r2, r3}, c = {c1, c2, c3};
int[][] perm = {{c[0],c[1],c[2]},{c[0],c[2],c[1]},{c[1],c[0],c[2]},{c[1],c[2],c[0]},{c[2],c[0],c[1]},{c[2],c[1],c[0]}};
for (int[] p : perm) {
int sum = 0;
for (int i = 0; i < 3; ++i) sum += board[r[i]][p[i]];
ans = Math.max(ans, sum);
}
}
return ans;
}
}
classSolution {
funmaxValueSum(board: Array<IntArray>): Int {
val m = board.size
val n = board[0].size
var ans = Int.MIN_VALUE
for (r1 in0 until m)
for (r2 in r1+1 until m)
for (r3 in r2+1 until m)
for (c1 in0 until n)
for (c2 in c1+1 until n)
for (c3 in c2+1 until n) {
val r = listOf(r1, r2, r3)
val c = listOf(c1, c2, c3)
val perm = listOf(
listOf(c[0],c[1],c[2]), listOf(c[0],c[2],c[1]), listOf(c[1],c[0],c[2]),
listOf(c[1],c[2],c[0]), listOf(c[2],c[0],c[1]), listOf(c[2],c[1],c[0])
)
for (p in perm) {
var sum = 0for (i in0..2) sum += board[r[i]][p[i]]
ans = maxOf(ans, sum)
}
}
return ans
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
from typing import List
classSolution:
defmaximumValueSum(self, board: List[List[int]]) -> int:
from itertools import combinations, permutations
m, n = len(board), len(board[0])
ans = float('-inf')
for rows in combinations(range(m), 3):
for cols in combinations(range(n), 3):
for perm in permutations(cols):
s = sum(board[rows[i]][perm[i]] for i in range(3))
ans = max(ans, s)
return ans
impl Solution {
pubfnmax_value_sum(board: Vec<Vec<i32>>) -> i32 {
let m = board.len();
let n = board[0].len();
letmut ans =i32::MIN;
for r1 in0..m {
for r2 in r1+1..m {
for r3 in r2+1..m {
for c1 in0..n {
for c2 in c1+1..n {
for c3 in c2+1..n {
let r = [r1, r2, r3];
let c = [c1, c2, c3];
let perms = [
[c[0],c[1],c[2]],[c[0],c[2],c[1]],[c[1],c[0],c[2]],
[c[1],c[2],c[0]],[c[2],c[0],c[1]],[c[2],c[1],c[0]]
];
for p in&perms {
let sum = (0..3).map(|i| board[r[i]][p[i]]).sum();
ans = ans.max(sum);
}
}
}
}
}
}
}
ans
}
}