lc-go/solutions/17/q1727/solution.go
2026-03-19 15:30:30 +09:00

33 lines
745 B
Go

// Package q1727 implements a solution for https://leetcode.com/problems/largest-submatrix-with-rearrangements/
package q1727
import "slices"
func largestSubmatrix(matrix [][]int) int {
maxArea := 0
// Count from the bottom the length of all contiguous segments of every column.
for col := range matrix[0] {
ones := 0
for row := len(matrix) - 1; row >= 0; row-- {
if matrix[row][col] == 0 {
ones = 0
} else {
ones += 1
matrix[row][col] = ones
}
}
}
for _, row := range matrix {
slices.SortFunc(row, func(a, b int) int { return b - a })
minLen := row[0]
for col := range row {
minLen = min(minLen, row[col])
maxArea = max(maxArea, minLen*(col+1))
}
}
return maxArea
}
var _ = largestSubmatrix