comments | difficulty | edit_url | rating | source | tags | |||||
---|---|---|---|---|---|---|---|---|---|---|
true |
困难 |
2402 |
第 413 场周赛 Q3 |
|
给你一个由正整数构成的二维矩阵 grid
。
你需要从矩阵中选择 一个或多个 单元格,选中的单元格应满足以下条件:
- 所选单元格中的任意两个单元格都不会处于矩阵的 同一行。
- 所选单元格的值 互不相同。
你的得分为所选单元格值的总和。
返回你能获得的 最大 得分。
示例 1:
示例 2:
提示:
1 <= grid.length, grid[i].length <= 10
1 <= grid[i][j] <= 100
我们定义
我们首先对矩阵进行预处理,使用一个哈希表
对于状态
最后我们返回
时间复杂度
class Solution:
def maxScore(self, grid: List[List[int]]) -> int:
g = defaultdict(set)
mx = 0
for i, row in enumerate(grid):
for x in row:
g[x].add(i)
mx = max(mx, x)
m = len(grid)
f = [[0] * (1 << m) for _ in range(mx + 1)]
for i in range(1, mx + 1):
for j in range(1 << m):
f[i][j] = f[i - 1][j]
for k in g[i]:
if j >> k & 1:
f[i][j] = max(f[i][j], f[i - 1][j ^ 1 << k] + i)
return f[-1][-1]
class Solution {
public int maxScore(List<List<Integer>> grid) {
int m = grid.size();
int mx = 0;
boolean[][] g = new boolean[101][m + 1];
for (int i = 0; i < m; ++i) {
for (int x : grid.get(i)) {
g[x][i] = true;
mx = Math.max(mx, x);
}
}
int[][] f = new int[mx + 1][1 << m];
for (int i = 1; i <= mx; ++i) {
for (int j = 0; j < 1 << m; ++j) {
f[i][j] = f[i - 1][j];
for (int k = 0; k < m; ++k) {
if (g[i][k] && (j >> k & 1) == 1) {
f[i][j] = Math.max(f[i][j], f[i - 1][j ^ 1 << k] + i);
}
}
}
}
return f[mx][(1 << m) - 1];
}
}
class Solution {
public:
int maxScore(vector<vector<int>>& grid) {
int m = grid.size();
int mx = 0;
bool g[101][11]{};
for (int i = 0; i < m; ++i) {
for (int x : grid[i]) {
g[x][i] = true;
mx = max(mx, x);
}
}
int f[mx + 1][1 << m];
memset(f, 0, sizeof(f));
for (int i = 1; i <= mx; ++i) {
for (int j = 0; j < 1 << m; ++j) {
f[i][j] = f[i - 1][j];
for (int k = 0; k < m; ++k) {
if (g[i][k] && (j >> k & 1) == 1) {
f[i][j] = max(f[i][j], f[i - 1][j ^ 1 << k] + i);
}
}
}
}
return f[mx][(1 << m) - 1];
}
};
func maxScore(grid [][]int) int {
m := len(grid)
mx := 0
g := [101][11]bool{}
for i, row := range grid {
for _, x := range row {
g[x][i] = true
mx = max(mx, x)
}
}
f := make([][]int, mx+1)
for i := range f {
f[i] = make([]int, 1<<m)
}
for i := 1; i <= mx; i++ {
for j := 0; j < 1<<m; j++ {
f[i][j] = f[i-1][j]
for k := 0; k < m; k++ {
if g[i][k] && (j>>k&1) == 1 {
f[i][j] = max(f[i][j], f[i-1][j^1<<k]+i)
}
}
}
}
return f[mx][1<<m-1]
}
function maxScore(grid: number[][]): number {
const m = grid.length;
let mx = 0;
const g: boolean[][] = Array.from({ length: 101 }, () => Array(m + 1).fill(false));
for (let i = 0; i < m; ++i) {
for (const x of grid[i]) {
g[x][i] = true;
mx = Math.max(mx, x);
}
}
const f: number[][] = Array.from({ length: mx + 1 }, () => Array(1 << m).fill(0));
for (let i = 1; i <= mx; ++i) {
for (let j = 0; j < 1 << m; ++j) {
f[i][j] = f[i - 1][j];
for (let k = 0; k < m; ++k) {
if (g[i][k] && ((j >> k) & 1) === 1) {
f[i][j] = Math.max(f[i][j], f[i - 1][j ^ (1 << k)] + i);
}
}
}
}
return f[mx][(1 << m) - 1];
}