正反两次扫描|单调性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;

}

};

相关推荐
vibecoding日记5 小时前
双非如何快速入职字节等大厂大模型?真实案例分析:推理优化和投机解码
算法·求职·大模型工程师
yszaygr21387 小时前
Verilog参数化游程编码RLE模块
算法
望易8 小时前
刚设计的大模型架构-双域耦合认知框架
算法·架构
复杂网络12 小时前
多个 Claude Code 与多个 Codex 协同工作:设计与实现方案
算法
HjhIron1 天前
面试常客:字符串算法从入门到进阶
算法·面试
吴佳浩1 天前
DeepSeek DSpark:Confidence-Scheduled Speculative Decoding 技术解析
人工智能·算法·deepseek
触底反弹1 天前
🧠 搞懂 Token,才算真正入门大模型——从分词原理到 Embedding 语义实战
javascript·人工智能·算法
vivo互联网技术1 天前
ICLR 2026 | 基于后验采样的图像恢复方法LearnIR:人脸去阴影、去雾
人工智能·算法·aigc
浮生望1 天前
JS字符串与回文算法:从包装类到双指针的面试进阶之路
javascript·算法