lc3047
遍历所有矩形对,计算每对矩形的交集区域,取++交集的宽高min++作为可能的正方形边长,记录最大边长并返回其平方值。

class Solution {
public:
long long largestSquareArea(vector<vector<int>>& bottomLeft, vector<vector<int>>& topRight) {
int max_side = 0;
for (int i = 0; i < bottomLeft.size(); i++) {
auto& b1 = bottomLefti;
auto& t1 = topRighti;
for (int j = 0; j < i; j++) {
auto& b2 = bottomLeftj;
auto& t2 = topRightj;
int width = min(t10, t20) - max(b10, b20);
int height = min(t11, t21) - max(b11, b21);
++int side = min(width, height);
max_side = max(max_side, side);++
}
}
return 1LL * max_side * max_side;
}
};
单调性cut

class Solution {
public:
long long largestSquareArea(vector<vector<int>>& bottomLeft, vector<vector<int>>& topRight) {
int max_side = 0;
for (int i = 0; i < bottomLeft.size(); i++) {
auto& b1 = bottomLefti;
auto& t1 = topRighti;
++if (t10 - b10 <= max_side || t11 - b11 <= max_side)
continue;++ // 最优性剪枝:max_side 不可能变大
for (int j = 0; j < i; j++) {
auto& b2 = bottomLeftj;
auto& t2 = topRightj;
int width = min(t10, t20) - max(b10, b20); // 右上横坐标 - 左下横坐标
int height = min(t11, t21) - max(b11, b21); // 右上纵坐标 - 左下纵坐标
int side = min(width, height);
max_side = max(max_side, side);
}
}
return 1LL * max_side * max_side;
}
};
lc3796
正反两次扫描

class Solution {
public:
int findMaxVal(int n, vector<vector<int>>& restrictions, vector<int>& diff) {
vector<int> max_val(n, INT_MAX);
for (auto& r : restrictions)
max_valr\[0] = r1;
vector<int> a(n);
for (int i = 0; i < n - 1; i++) {
ai + 1 = min(ai + diffi, max_vali + 1);
}
for (int i = n - 2; i > 0; i--) {
ai = min(ai, ai + 1 + diffi);
}
return ranges::max(a);
}
};
lc3795
hash+滑窗
class Solution {
public:
int minLength(vector<int>& nums, int k) {
unordered_map<int, int> cnt;
int sum = 0;
int left = 0;
int ans = INT_MAX;
for (int i = 0; i < nums.size(); i++) {
// 1. 入
int x = numsi;
cntx++;
if (cntx == 1)
sum += x;
while (sum >= k) {
// 2. 更新答案
ans = min(ans, i - left + 1);
// 3. 出
int out = numsleft;
cntout--;
if (cntout == 0)
sum -= out;
left++;
}
}
return ans == INT_MAX ? -1 : ans;
}
};