回溯+位运算|前缀和优化背包

lc2402

双堆模拟

用两个优先队列分别管理空闲会议室和使用中会议室的结束时间与编号

按会议开始时间排序后依次安排会议

统计各会议室使用次数并返回使用最多的那个会议室编号

class Solution {

public:

int mostBooked(int n, vector<vector<int>>& meetings) {

ranges::sort(meetings, {}, \[\](auto& m) { return m0; });

priority_queue<int, vector<int>, greater<>> idle; // 会议室编号

for (int i = 0; i < n; i++) {

idle.push(i);

}

priority_queue<pair<long long, int>, vector<pair<long long, int>>, greater<>> using_; // (结束时间,会议室编号)

vector<int> cnt(n); // 会议室的开会次数

for (auto& m : meetings) {

long long start = m0, end = m1;

// 在 start 时刻空出来的会议室

while (!using_.empty() && using_.top().first <= start) {

idle.push(using_.top().second);

using_.pop();

}

int i;

if (!idle.empty()) { // 有空闲的会议室

i = idle.top();

idle.pop();

} else { // 没有空闲的会议室

// 弹出一个最早结束的会议室(若有多个同时结束,弹出编号最小的会议室)

auto e, room = using_.top();

i = room;

using_.pop();

end += e - start; // 更新当前会议的结束时间

}

using_.emplace(end, i); // 使用一个会议室

cnti++;

}

return ranges::max_element(cnt) - cnt.begin();

}

};

lc756

状态压缩+记忆化DFS

将字符映射为数字并压缩存储金字塔每一层状态,利用允许++组合表递归尝试构建上一层++,判断能否从底部生成合法金字塔

class Solution {

public:

bool pyramidTransition(string bottom, vector<string>& allowed) {

vector<int> groups77;

for (auto& s : allowed) {

// A~F -> 1~6

groupss\[0 & 31]s\[1 & 31].push_back(s2 & 31);

}

int n = bottom.size();

vector<int> pyramid(n);

for (int i = 0; i < n; i++) {

pyramidn - 1 |= (bottomi & 31) << (i * 3); // 等价于 pyramidn-1i = bottomi&31

}

vector<uint8_t> vis(1 << ((n - 1) * 3));

auto dfs = \&(this auto&& dfs, int i, int j) -> bool {

if (i < 0) {

return true;

}

if (vispyramid\[i]) {

return false;

}

if (j == i + 1) {

vispyramid\[i] = true;

return dfs(i - 1, 0);

}

for (int top : groupspyramid\[i + 1 >> (j * 3) & 7]pyramid\[i + 1 >> ((j + 1) * 3) & 7]) {

pyramidi &= ~(7 << (j * 3)); // 清除之前填的字母,等价于 pyramidij = 0

pyramidi |= top << (j * 3); // 等价于 pyramidij = top

if (dfs(i, j + 1)) {

return true;

}

}

return false;

};

return dfs(n - 2, 0);

}

};

lc3333

前缀和优化多重背包

class Solution {

public:

int possibleStringCount(string word, int k) {

int n = word.size();

if (n < k) { // 无法满足要求

return 0;

}

const int MOD = 1'000'000'007;

vector<int> cnts;

long long ans = 1;

int cnt = 0;

for (int i = 0; i < n; i++) {

cnt++;

if (i == n - 1 || wordi != wordi + 1) {

// 如果 cnt = 1,这组字符串必选,无需参与计算

if (cnt > 1) {

if (k > 0) {

cnts.push_back(cnt - 1);

}

ans = ans * cnt % MOD;

}

k--; // 注意这里把 k 减小了

cnt = 0;

}

}

if (k <= 0) {

return ans;

}

int m = cnts.size();

vector f(m + 1, vector<int>(k));

ranges::fill(f0, 1);

vector<int> s(k + 1);

for (int i = 0; i < m; i++) {

// 计算 fi 的前缀和数组 s

for (int j = 0; j < k; j++) {

sj + 1 = (sj + fij) % MOD;

}

// 计算子数组和

for (int j = 0; j < k; j++) {

fi + 1j = (sj + 1 - smax(j - cnts\[i, 0)]) % MOD;

}

}

return (ans - fmk - 1 + MOD) % MOD; // 保证结果非负

}

};

相关推荐
JieE21214 小时前
LeetCode 56. 合并区间|超清晰 JS 图解思路,面试高频区间题
javascript·算法·面试
Jack201 天前
HarmonyOS开发中错误处理策略:网络异常统一处理
算法
小小杨树1 天前
读懂色彩:拍照调色不再难
算法·计算机视觉·配色
JieE2122 天前
LeetCode 226. 翻转二叉树|JS 递归超详细拆解,二叉树入门经典题
javascript·算法
JieE2122 天前
LeetCode 104. 二叉树的最大深度|递归思路超详细拆解
javascript·算法
vivo互联网技术2 天前
CVPR 2026 | 全新强化学习框架 BeautyGRPO:重塑真实人像
算法·大模型·cvpr·影像
Darling噜啦啦2 天前
列表转树算法深度解析:从 Map 到 Reduce 的两种实现,面试高频考点
数据结构·算法·面试
用户497863050732 天前
(一)小红的数组操作
算法·编程语言
怕浪猫2 天前
Electron 系列文章封面图
算法·架构·前端框架