add new solutions

This commit is contained in:
Yiyang Kang 2026-03-19 15:29:54 +09:00
parent f297e11859
commit 4720cbefc4
8 changed files with 290 additions and 0 deletions

View file

@ -0,0 +1,33 @@
// 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