2975: 移除栅栏得到的正方形田地的最大面积
思路:暴力枚举正方形所有可能的边长
水平栅栏和垂直栅栏分开计算。
- 对于水平栅栏,++任意两个栅栏之间的距离++(中间的栅栏全部删除)都可能是正方形的边长,存到一个哈希表 hSet 中。
- 对于垂直栅栏,任意两个栅栏之间的距离(中间的栅栏全部删除)都可能是正方形的边长,存到一个哈希表 vSet 中。
答案是 hSet 和 vSet ++交集中的最大值++的平方,记得返回之前取模。如果交集为空,返回 −1。

class Solution {
public:
int maximizeSquareArea(int m, int n, vector<int>& hFences, vector<int>& vFences) {
constexpr int MOD=1'000'000'007;
auto f=[&](vector<int>& nums,int x)->unordered_set<int>{
unordered_set<int> st; //正方形可能边长
nums.push_back(1);
nums.push_back(x);
sort(nums.begin(),nums.end());
for(int i=0;i<nums.size();i++){
for(int j=i+1;j<nums.size();j++){
int d=nums[j]-nums[i];
if(d>0) st.insert(d);
}
}
return st;
};
unordered_set<int> h=f(hFences,m);
unordered_set<int> v=f(vFences,n);
int best=-1;
for(int x:h) if(v.count(x)) best=max(best,x);
return best==-1 ? -1:(long long)best*best%MOD;
}
};