42 lines
988 B
Go
42 lines
988 B
Go
package q3047
|
|
|
|
import "slices"
|
|
|
|
func intersectingSquareArea(bl1, tr1, bl2, tr2 []int) int64 {
|
|
l1, r1, l2, r2 := bl1[0], tr1[0], bl2[0], tr2[0]
|
|
iW := max(0, min(r1, r2)-max(l1, l2))
|
|
|
|
l1, r1, l2, r2 = bl1[1], tr1[1], bl2[1], tr2[1]
|
|
iH := max(0, min(r1, r2)-max(l1, l2))
|
|
|
|
edge := min(iW, iH)
|
|
return int64(edge * edge)
|
|
}
|
|
|
|
func largestSquareArea(bottomLeft [][]int, topRight [][]int) int64 {
|
|
squares := make([]int, len(bottomLeft))
|
|
for i := range squares {
|
|
squares[i] = i
|
|
}
|
|
slices.SortFunc(squares, // bottom-up order
|
|
func(a, b int) int { return bottomLeft[a][1] - bottomLeft[b][1] })
|
|
|
|
var largest int64
|
|
|
|
for i := range squares {
|
|
for j := i + 1; j < len(squares); j++ {
|
|
yI, yJ := topRight[squares[i]][1], bottomLeft[squares[j]][1]
|
|
if yJ >= yI {
|
|
break
|
|
}
|
|
|
|
largest = max(largest, intersectingSquareArea(
|
|
bottomLeft[squares[i]], topRight[squares[i]],
|
|
bottomLeft[squares[j]], topRight[squares[j]],
|
|
))
|
|
}
|
|
}
|
|
return largest
|
|
}
|
|
|
|
var _ = largestSquareArea
|