add new solutions
This commit is contained in:
parent
f297e11859
commit
4720cbefc4
8 changed files with 290 additions and 0 deletions
33
solutions/17/q1727/solution.go
Normal file
33
solutions/17/q1727/solution.go
Normal 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
|
||||
Loading…
Add table
Add a link
Reference in a new issue