LeetCode 题解工作台
移除栅栏得到的正方形田地的最大面积
有一个大型的 (m - 1) x (n - 1) 矩形田地,其两个对角分别是 (1, 1) 和 (m, n) ,田地内部有一些水平栅栏和垂直栅栏,分别由数组 hFences 和 vFences 给出。 水平栅栏为坐标 (hFences[i], 1) 到 (hFences[i], n) ,垂直栅栏为坐…
3
题型
6
代码语言
3
相关题
当前训练重点
中等 · 数组·哈希·扫描
答案摘要
我们可以枚举 中的任意两条水平栅栏 和 ,计算 和 之间的距离 ,记录在哈希表 中,然后枚举 中的任意两条垂直栅栏 和 ,计算 和 之间的距离 ,记录在哈希表 中,最后遍历哈希表 ,如果 中的某个距离 在哈希表 中也存在,那么说明存在一个正方形田地,其边长为 ,面积为 ,我们只需要取最大的 ,求 $d^2 \bmod 10^9 + 7$ 即可。 时间复杂度 $O(h^2 …
Interview AiBoxInterview AiBox 实时 AI 助手,陪你讲清 数组·哈希·扫描 题型思路
题目描述
有一个大型的 (m - 1) x (n - 1) 矩形田地,其两个对角分别是 (1, 1) 和 (m, n) ,田地内部有一些水平栅栏和垂直栅栏,分别由数组 hFences 和 vFences 给出。
水平栅栏为坐标 (hFences[i], 1) 到 (hFences[i], n),垂直栅栏为坐标 (1, vFences[i]) 到 (m, vFences[i]) 。
返回通过 移除 一些栅栏(可能不移除)所能形成的最大面积的 正方形 田地的面积,或者如果无法形成正方形田地则返回 -1。
由于答案可能很大,所以请返回结果对 109 + 7 取余 后的值。
注意:田地外围两个水平栅栏(坐标 (1, 1) 到 (1, n) 和坐标 (m, 1) 到 (m, n) )以及两个垂直栅栏(坐标 (1, 1) 到 (m, 1) 和坐标 (1, n) 到 (m, n) )所包围。这些栅栏 不能 被移除。
示例 1:

输入:m = 4, n = 3, hFences = [2,3], vFences = [2] 输出:4 解释:移除位于 2 的水平栅栏和位于 2 的垂直栅栏将得到一个面积为 4 的正方形田地。
示例 2:

输入:m = 6, n = 7, hFences = [2], vFences = [4] 输出:-1 解释:可以证明无法通过移除栅栏形成正方形田地。
提示:
3 <= m, n <= 1091 <= hFences.length, vFences.length <= 6001 < hFences[i] < m1 < vFences[i] < nhFences和vFences中的元素是唯一的。
解题思路
方法一:枚举
我们可以枚举 中的任意两条水平栅栏 和 ,计算 和 之间的距离 ,记录在哈希表 中,然后枚举 中的任意两条垂直栅栏 和 ,计算 和 之间的距离 ,记录在哈希表 中,最后遍历哈希表 ,如果 中的某个距离 在哈希表 中也存在,那么说明存在一个正方形田地,其边长为 ,面积为 ,我们只需要取最大的 ,求 即可。
时间复杂度 ,空间复杂度 。其中 和 分别是 和 的长度。
class Solution:
def maximizeSquareArea(
self, m: int, n: int, hFences: List[int], vFences: List[int]
) -> int:
def f(nums: List[int], k: int) -> Set[int]:
nums.extend([1, k])
nums.sort()
return {b - a for a, b in combinations(nums, 2)}
mod = 10**9 + 7
hs = f(hFences, m)
vs = f(vFences, n)
ans = max(hs & vs, default=0)
return ans**2 % mod if ans else -1
复杂度分析
| 指标 | 值 |
|---|---|
| 时间 | complexity depends on the final approach, particularly the number of fences and the efficiency of the array scanning and hash lookup steps. Space complexity depends on the storage needed for the transformed fence arrays and hash table lookups. |
| 空间 | Depends on the final approach |
面试官常问的追问
外企场景- question_mark
The candidate should focus on efficiently scanning the arrays and utilizing hash lookups to avoid brute force solutions.
- question_mark
Look for the candidate to demonstrate how to handle the large constraints effectively, especially with hash tables and efficient array manipulation.
- question_mark
The candidate should consider and discuss edge cases, especially situations where no square can be formed.
常见陷阱
外企场景- error
Failing to handle edge cases where no square can be formed, leading to incorrect or missing outputs.
- error
Not efficiently calculating the differences between consecutive fences, which could result in slow performance for larger inputs.
- error
Overcomplicating the approach with unnecessary nested loops or brute force methods instead of leveraging array scanning and hash lookup.
进阶变体
外企场景- arrow_right_alt
What if you were given the ability to remove only specific fences, not all fences?
- arrow_right_alt
How would the approach change if the dimensions of the field were not fixed and could vary during the problem?
- arrow_right_alt
What if there were additional constraints on the positioning of the fences or the size of the field?