2713. 矩阵中严格递增的单元格数

矩阵中严格递增的单元格数

解法一: 动态规划

go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
func maxIncreasingCells(mat [][]int) int {
type point struct {
x int
y int
}
m := len(mat)
n := len(mat[0])
points := make(map[int][]point)
for i := 0; i < m; i++ {
for j := 0; j < n; j++ {
points[mat[i][j]] = append(points[mat[i][j]], point{
x: i,
y: j,
})
}
}
pointKeys := make([]int, 0, len(points))
for key := range points {
pointKeys = append(pointKeys, key)
}
slices.Sort(pointKeys)
maxRows := make([]int, m)
maxCols := make([]int, n)
for _, key := range pointKeys {
maxNums := make([]int, len(points[key]))
for i, point := range points[key] {
maxNums[i] = max(maxRows[point.x], maxCols[point.y]) + 1
}
for i, point := range points[key] {
maxRows[point.x] = max(maxRows[point.x], maxNums[i])
maxCols[point.y] = max(maxCols[point.y], maxNums[i])
}
}
return slices.Max(maxRows)
}
作者

wuhunyu

发布于

2024-06-19

更新于

2025-01-15

许可协议