LeetCode 题解工作台

移除栅栏得到的正方形田地的最大面积

有一个大型的 (m - 1) x (n - 1) 矩形田地,其两个对角分别是 (1, 1) 和 (m, n) ,田地内部有一些水平栅栏和垂直栅栏,分别由数组 hFences 和 vFences 给出。 水平栅栏为坐标 (hFences[i], 1) 到 (hFences[i], n) ,垂直栅栏为坐…

category

3

题型

code_blocks

6

代码语言

hub

3

相关题

当前训练重点

中等 · 数组·哈希·扫描

bolt

答案摘要

我们可以枚举 中的任意两条水平栅栏 和 ,计算 和 之间的距离 ,记录在哈希表 中,然后枚举 中的任意两条垂直栅栏 和 ,计算 和 之间的距离 ,记录在哈希表 中,最后遍历哈希表 ,如果 中的某个距离 在哈希表 中也存在,那么说明存在一个正方形田地,其边长为 ,面积为 ,我们只需要取最大的 ,求 $d^2 \bmod 10^9 + 7$ 即可。 时间复杂度 $O(h^2 …

Interview AiBox logo

Interview AiBox 实时 AI 助手,陪你讲清 数组·哈希·扫描 题型思路

试试 AiBox 面试助手arrow_forward
description

题目描述

有一个大型的 (m - 1) x (n - 1) 矩形田地,其两个对角分别是 (1, 1)(m, n) ,田地内部有一些水平栅栏和垂直栅栏,分别由数组 hFencesvFences 给出。

水平栅栏为坐标 (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 <= 109
  • 1 <= hFences.length, vFences.length <= 600
  • 1 < hFences[i] < m
  • 1 < vFences[i] < n
  • hFencesvFences 中的元素是唯一的。
lightbulb

解题思路

方法一:枚举

我们可以枚举 hFences\textit{hFences} 中的任意两条水平栅栏 aabb,计算 aabb 之间的距离 dd,记录在哈希表 hshs 中,然后枚举 vFences\textit{vFences} 中的任意两条垂直栅栏 ccdd,计算 ccdd 之间的距离 dd,记录在哈希表 vsvs 中,最后遍历哈希表 hshs,如果 hshs 中的某个距离 dd 在哈希表 vsvs 中也存在,那么说明存在一个正方形田地,其边长为 dd,面积为 d2d^2,我们只需要取最大的 dd,求 d2mod109+7d^2 \bmod 10^9 + 7 即可。

时间复杂度 O(h2+v2)O(h^2 + v^2),空间复杂度 O(h2+v2)O(h^2 + v^2)。其中 hhvv 分别是 hFences\textit{hFences}vFences\textit{vFences} 的长度。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
speed

复杂度分析

指标
时间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
psychology

面试官常问的追问

外企场景
  • 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.

warning

常见陷阱

外企场景
  • 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.

swap_horiz

进阶变体

外企场景
  • 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?

help

常见问题

外企场景

移除栅栏得到的正方形田地的最大面积题解:数组·哈希·扫描 | LeetCode #2975 中等