正反两次扫描|单调性cut

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;

}

};

相关推荐
A加ITIL教练长河achotsao30 分钟前
ITIL Master 战略领导者课后系列分享:AI编程正在重新划分研发与运维的边界
算法
hahaha601631 分钟前
FPGA+ARM实现AWB的全流程
人工智能·嵌入式硬件·算法·计算机视觉·fpga开发
李洱32 分钟前
LangChain 和 LangGraph 的区别:核心原理与使用场景
算法
夏幻灵34 分钟前
每日一题:LeetCode 206:反转链表——三指针与原地反转详解
算法·leetcode·链表
岁月蹉跎的一杯酒1 小时前
CCM-可调颜色校正与噪声权衡
图像处理·算法·isp
在所不辞兄2 小时前
选择隐式方法求解刚性ODE
算法
码爸2 小时前
贪心算法介绍与示例
算法·贪心算法
-dzk-2 小时前
【多维动态规划】LC 64.最小路径和
算法·动态规划
垆边人似月.3 小时前
华为机试题 :字符串压缩
算法·华为
ggb喔3 小时前
52pojie 的图片工具:为什么无损转换后体积涨了 6 倍、照片方向还是歪的
图像处理·python·算法·计算机视觉·图形渲染·媒体