Leetcode 2975. Maximum Square Area by Removing Fences From a Field

  • [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。

相关推荐
Espresso Macchiato1 个月前
Leetcode 3255. Find the Power of K-Size Subarrays II
leetcode·leetcode medium·leetcode 3255·leetcode 3254·leetcode周赛137
Espresso Macchiato2 个月前
Leetcode 3240. Minimum Number of Flips to Make Binary Grid Palindromic II
leetcode·leetcode medium·回文·leetcode 3240·leetcode双周赛136
Espresso Macchiato2 个月前
Leetcode 3234. Count the Number of Substrings With Dominant Ones
排列组合·leetcode medium·容斥原理·leetcode 3234·leetcode周赛408
Espresso Macchiato3 个月前
Leetcode 3201. Find the Maximum Length of Valid Subsequence I
leetcode medium·leetcode题解·leetcode 3201·leetcode周赛404
Espresso Macchiato3 个月前
Leetcode 3196. Maximize Total Cost of Alternating Subarrays
leetcode·动态规划·leetcode medium·leetcode周赛403·leetcode 3196
Espresso Macchiato3 个月前
Leetcode 3195. Find the Minimum Area to Cover All Ones I
leetcode·leetcode medium·leetcode题解·leetcode 3195·leetcode周赛403
Espresso Macchiato3 个月前
Leetcode 3186. Maximum Total Damage With Spell Casting
动态规划·leetcode medium·leetcode题解·leetcode 3186·leetcode周赛402
Espresso Macchiato3 个月前
Leetcode 3175. Find The First Player to win K Games in a Row
leetcode medium·leetcode题解·leetcode 3175·leetcode双周赛132
Espresso Macchiato4 个月前
Leetcode 3143. Maximum Points Inside the Square
leetcode medium·leetcode题解·leetcode双周赛130·leetcode 3143
Espresso Macchiato4 个月前
Leetcode 3128. Right Triangles
leetcode medium·leetcode题解·leetcode 3128·leetcode双周赛129