Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* @Author: LetMeFly
* @Date: 2026-01-17 21:02:24
* @LastEditors: LetMeFly.xyz
* @LastEditTime: 2026-01-17 21:08:43
*/
#if defined(_WIN32) || defined(__APPLE__)
#include "_[1,2]toVector.h"
#endif

class Solution {
private:
unordered_set<int> get(int n, vector<int>& v) {
v.push_back(1);
v.push_back(n);
unordered_set<int> ans;
for (int i = 0; i < v.size(); i++) {
for (int j = i + 1; j < v.size(); j++) {
ans.insert(abs(v[i] - v[j]));
Comment thread
LetMeFly666 marked this conversation as resolved.
}
}
return ans;
}
public:
int maximizeSquareArea(int m, int n, vector<int>& hFences, vector<int>& vFences) {
unordered_set<int> va = get(m, hFences), vb = get(n, vFences);
int side = 0;
for (int a : va) {
if (vb.count(a)) {
side = max(side, a);
}
}
return side ? (long long) side * side % 1000000007 : -1;
}
};
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,12 @@
* @Author: LetMeFly
* @Date: 2026-01-17 19:45:30
* @LastEditors: LetMeFly.xyz
* @LastEditTime: 2026-01-17 19:51:43
* @LastEditTime: 2026-01-17 19:55:20
*/
#if defined(_WIN32) || defined(__APPLE__)
#include "_[1,2]toVector.h"
#endif

// THIS CANNOT BE ACCEPTED
typedef long long ll;

class Solution {
Expand All @@ -31,9 +30,29 @@ class Solution {
for (int j = i + 1; j < bottomLeft.size(); j++) {
ll dx = getDiff(bottomLeft[i][0], topRight[i][0], bottomLeft[j][0], topRight[j][0]);
ll dy = getDiff(bottomLeft[i][1], topRight[i][1], bottomLeft[j][1], topRight[j][1]);
ans = max(ans, dx * dy);
ans = max(ans, min(dx, dy) * min(dx, dy));
}
}
return ans;
}
};
};


#if defined(_WIN32) || defined(__APPLE__)
/*
[[1,1],[2,2],[3,1]]
[[3,3],[4,4],[6,6]]

1
*/
int main() {
string a, b;
while (cin >> a >> b) {
vector<vector<int>> va = stringToVectorVector(a);
vector<vector<int>> vb = stringToVectorVector(b);
Solution sol;
cout << sol.largestSquareArea(va, vb) << endl;
}
return 0;
}
#endif

This file was deleted.

1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -963,6 +963,7 @@
|2965.找出缺失和重复的数字|简单|<a href="https://leetcode.cn/problems/find-missing-and-repeated-values/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2024/05/31/LeetCode%202965.%E6%89%BE%E5%87%BA%E7%BC%BA%E5%A4%B1%E5%92%8C%E9%87%8D%E5%A4%8D%E7%9A%84%E6%95%B0%E5%AD%97/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/139357662" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/find-missing-and-repeated-values/solutions/2796819/letmefly-2965zhao-chu-que-shi-he-zhong-f-p2rn/" target="_blank">LeetCode题解</a>|
|2970.统计移除递增子数组的数目I|简单|<a href="https://leetcode.cn/problems/count-the-number-of-incremovable-subarrays-i/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2024/07/10/LeetCode%202970.%E7%BB%9F%E8%AE%A1%E7%A7%BB%E9%99%A4%E9%80%92%E5%A2%9E%E5%AD%90%E6%95%B0%E7%BB%84%E7%9A%84%E6%95%B0%E7%9B%AEI/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/140310063" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/count-the-number-of-incremovable-subarrays-i/solutions/2836851/letmefly-2970tong-ji-yi-chu-di-zeng-zi-s-3d2z/" target="_blank">LeetCode题解</a>|
|2974.最小数字游戏|简单|<a href="https://leetcode.cn/problems/minimum-number-game/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2024/07/12/LeetCode%202974.%E6%9C%80%E5%B0%8F%E6%95%B0%E5%AD%97%E6%B8%B8%E6%88%8F/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/140365205" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/minimum-number-game/solutions/2840437/letmefly-2974zui-xiao-shu-zi-you-xi-pai-ib5em/" target="_blank">LeetCode题解</a>|
|2975.移除栅栏得到的正方形田地的最大面积|中等|<a href="https://leetcode.cn/problems/maximum-square-area-by-removing-fences-from-a-field/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2026/01/17/LeetCode%202975.%E7%A7%BB%E9%99%A4%E6%A0%85%E6%A0%8F%E5%BE%97%E5%88%B0%E7%9A%84%E6%AD%A3%E6%96%B9%E5%BD%A2%E7%94%B0%E5%9C%B0%E7%9A%84%E6%9C%80%E5%A4%A7%E9%9D%A2%E7%A7%AF/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/157068738" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/maximum-square-area-by-removing-fences-from-a-field/solutions/3882988/letmefly-2975yi-chu-zha-lan-de-dao-de-zh-wua2/" target="_blank">LeetCode题解</a>|
|2982.找出出现至少三次的最长特殊子字符串II|中等|<a href="https://leetcode.cn/problems/find-longest-special-substring-that-occurs-thrice-ii/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2024/05/30/LeetCode%202982.%E6%89%BE%E5%87%BA%E5%87%BA%E7%8E%B0%E8%87%B3%E5%B0%91%E4%B8%89%E6%AC%A1%E7%9A%84%E6%9C%80%E9%95%BF%E7%89%B9%E6%AE%8A%E5%AD%90%E5%AD%97%E7%AC%A6%E4%B8%B2II/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/139334864" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/find-longest-special-substring-that-occurs-thrice-ii/solutions/2795996/letmefly-2982zhao-chu-chu-xian-zhi-shao-9ubg1/" target="_blank">LeetCode题解</a>|
|2999.统计强大整数的数目|困难|<a href="https://leetcode.cn/problems/count-the-number-of-powerful-integers/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2025/04/12/LeetCode%202999.%E7%BB%9F%E8%AE%A1%E5%BC%BA%E5%A4%A7%E6%95%B4%E6%95%B0%E7%9A%84%E6%95%B0%E7%9B%AE/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/147191672" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/count-the-number-of-powerful-integers/solutions/3649695/letmefly-2999tong-ji-qiang-da-zheng-shu-jnya8/" target="_blank">LeetCode题解</a>|
|3000.对角线最长的矩形的面积|简单|<a href="https://leetcode.cn/problems/maximum-area-of-longest-diagonal-rectangle/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2025/08/26/LeetCode%203000.%E5%AF%B9%E8%A7%92%E7%BA%BF%E6%9C%80%E9%95%BF%E7%9A%84%E7%9F%A9%E5%BD%A2%E7%9A%84%E9%9D%A2%E7%A7%AF/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/150871371" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/maximum-area-of-longest-diagonal-rectangle/solutions/3764586/letmefly-3000dui-jiao-xian-zui-chang-de-cxvkc/" target="_blank">LeetCode题解</a>|
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
---
title: 2975.移除栅栏得到的正方形田地的最大面积:暴力枚举所有可能宽度
date: 2026-01-17 21:10:06
tags: [题解, LeetCode, 中等, 数组, 哈希表, set, 枚举, 暴力]
categories: [题解, LeetCode]
index_img: https://assets.leetcode.com/uploads/2023/11/05/screenshot-from-2023-11-05-22-40-25.png
---

# 【LetMeFly】2975.移除栅栏得到的正方形田地的最大面积:暴力枚举所有可能宽度

力扣题目链接:[https://leetcode.cn/problems/maximum-square-area-by-removing-fences-from-a-field/](https://leetcode.cn/problems/maximum-square-area-by-removing-fences-from-a-field/)

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

<p>水平栅栏为坐标 <code>(hFences[i], 1)</code> 到 <code>(hFences[i], n)</code>,垂直栅栏为坐标 <code>(1, vFences[i])</code> 到 <code>(m, vFences[i])</code> 。</p>

<p>返回通过<strong> 移除 </strong>一些栅栏(<strong>可能不移除</strong>)所能形成的最大面积的<strong> 正方形 </strong>田地的面积,或者如果无法形成正方形田地则返回 <code>-1</code>。</p>

<p>由于答案可能很大,所以请返回结果对 <code>10<sup>9</sup> + 7</code> <strong>取余</strong> 后的值。</p>

<p><strong>注意:</strong>田地外围两个水平栅栏(坐标 <code>(1, 1)</code> 到 <code>(1, n)</code> 和坐标 <code>(m, 1)</code> 到 <code>(m, n)</code> )以及两个垂直栅栏(坐标 <code>(1, 1)</code> 到 <code>(m, 1)</code> 和坐标 <code>(1, n)</code> 到 <code>(m, n)</code> )所包围。这些栅栏<strong> 不能</strong> 被移除。</p>

<p>&nbsp;</p>

<p><strong class="example">示例 1:</strong></p>

<p><img alt="" src="https://assets.leetcode.com/uploads/2023/11/05/screenshot-from-2023-11-05-22-40-25.png" /></p>

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

<p><strong class="example">示例 2:</strong></p>

<p><img alt="" src="https://assets.leetcode.com/uploads/2023/11/22/maxsquareareaexample1.png" style="width: 285px; height: 242px;" /></p>

<pre>
<strong>输入:</strong>m = 6, n = 7, hFences = [2], vFences = [4]
<strong>输出:</strong>-1
<strong>解释:</strong>可以证明无法通过移除栅栏形成正方形田地。
</pre>

<p>&nbsp;</p>

<p><strong>提示:</strong></p>

<ul>
<li><code>3 &lt;= m, n &lt;= 10<sup>9</sup></code></li>
<li><code>1 &lt;= hFences.length, vFences.length &lt;= 600</code></li>
<li><code>1 &lt; hFences[i] &lt; m</code></li>
<li><code>1 &lt; vFences[i] &lt; n</code></li>
<li><code>hFences</code> 和 <code>vFences</code> 中的元素是唯一的。</li>
</ul>



## 解题方法:暴力枚举

水平竖直单看一个方向,可能的边长有哪些?

> 任意两个栅栏之间的距离都可以是边长,可以二重循环栅栏位置并将其放入哈希表中。

水平竖直一块看,如果一个边长在水平方向有可能得到,在竖直方向也有可能得到,那么就有办法得到这个长度为边长的正方形。

也就是两个哈希表求个交并取最大就好了。

+ 时间复杂度$O((len(hFences) + len(vFences))^2)$,这是因为$(a+b)^2=a^2+b^2+2ab$,复杂度中等于$a^2+b^2+ab$
+ 空间复杂度$O(len(hFences) + len(vFences))$

### AC代码

#### C++

```cpp
/*
* @LastEditTime: 2026-01-17 21:08:43
*/
class Solution {
private:
unordered_set<int> get(int n, vector<int>& v) {
v.push_back(1);
v.push_back(n);
unordered_set<int> ans;
for (int i = 0; i < v.size(); i++) {
for (int j = i + 1; j < v.size(); j++) {
ans.insert(abs(v[i] - v[j]));
}
}
return ans;
}
public:
int maximizeSquareArea(int m, int n, vector<int>& hFences, vector<int>& vFences) {
unordered_set<int> va = get(m, hFences), vb = get(n, vFences);
int side = 0;
for (int a : va) {
if (vb.count(a)) {
side = max(side, a);
}
}
return side ? (long long) side * side % 1000000007 : -1;
}
};
```

> 同步发文于[CSDN](https://letmefly.blog.csdn.net/article/details/157068738)和我的[个人博客](https://blog.letmefly.xyz/),原创不易,转载经作者同意后请附上[原文链接](https://blog.letmefly.xyz/2026/01/17/LeetCode%202975.%E7%A7%BB%E9%99%A4%E6%A0%85%E6%A0%8F%E5%BE%97%E5%88%B0%E7%9A%84%E6%AD%A3%E6%96%B9%E5%BD%A2%E7%94%B0%E5%9C%B0%E7%9A%84%E6%9C%80%E5%A4%A7%E9%9D%A2%E7%A7%AF/)哦~
>
> 千篇源码题解[已开源](https://github.com/LetMeFly666/LeetCode)
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ title: 3047.求交集区域内的最大正方形面积:2层循环暴力枚举
date: 2026-01-17 20:05:07
tags: [题解, LeetCode, 中等, 几何, 数组, 数学]
categories: [题解, LeetCode]
index_img: https://assets.leetcode.com/uploads/2024/01/05/example12.png
---

# 【LetMeFly】3047.求交集区域内的最大正方形面积:2层循环暴力枚举
Expand Down