- [Leetcode 2975. Maximum Square Area by Removing Fences From a Field](#Leetcode 2975. Maximum Square Area by Removing Fences From a Field)
- [1. 解题思路](#1. 解题思路)
- [2. 代码实现](#2. 代码实现)
1. 解题思路
这一题思路上是比较直接的,就是直接求出横向和纵向上可能的interval的大小,然后取交集之后返回最大值即可。
2. 代码实现
给出python代码实现如下:
python
class Solution:
def maximizeSquareArea(self, m: int, n: int, hFences: List[int], vFences: List[int]) -> int:
MOD = 10**9+7
hFences = [1] + sorted(hFences) + [m]
k = len(hFences)
h = {hFences[j] - hFences[i] for i in range(k-1) for j in range(i+1, k)}
vFences = [1] + sorted(vFences) + [n]
k = len(vFences)
v = {vFences[j] - vFences[i] for i in range(k-1) for j in range(i+1, k)}
s = h & v
return -1 if len(s) == 0 else max(s)**2 % MOD
提交代码评测得到:耗时1192ms,占用内存56.1MB。