当前位置: 首页 > news >正文

pq|dfs|快排

lc1985

简单题 简单做

上堆/快排也行

按字符串长度降序、长度相同则按字典序降序排序数字字符串数组,返回第 k 大的元素。

class Solution {
public:
string kthLargestNumber(vector<string>& nums, int k) {
sort(nums.begin(), nums.end(),
[](string s1, string s2)->bool
{
if(s1.size() != s2.size()) return s1.size() > s2.size(); //先比字符串的长度
else return s1 > s2;//再比字符串的大小
});
return nums[k - 1];//返回第k个大的
}
};

优先队列

class Solution {
private:
static bool cmp(const string& s1, const string& s2) {
if(s1.length() != s2.length()) return s1.length() < s2.length();
for(int i = 0; i < s1.length(); ++i) {
if(s1[i] != s2[i])
return s1[i] < s2[i];
}
return false;
};
public:
string kthLargestNumber(vector<string>& nums, int k) {
priority_queue<string, vector<string>, decltype(&Solution::cmp)> q(cmp);
for(const auto& s: nums)
q.push(s);
while(!q.empty() && --k)
q.pop();
return q.top();
}
};

快排接口 优雅的比较函数😋

class Solution {
public:
static bool cmp(string s1, string s2) {
return s1.size() != s2.size() ? s1.size() > s2.size() : s1 > s2;
}

string kthLargestNumber(vector<string>& nums, int k)

{
nth_element(nums.begin(), nums.begin() + k - 1, nums.end(), cmp);
return nums[k - 1];
}
};

lc3331

感觉就是当图来做的,注意回溯处理

先建树,DFS回溯维护每个字符的最近祖先,把同字符子节点移到最近祖先下重构树

最后DFS统计每个节点的子树大小

class Solution {
public:
vector<int> findSubtreeSizes(vector<int>& parent, string s) {
int n = parent.size();
vector<vector<int>> g(n);
for (int i = 1; i < n; i++)
g[parent[i]].push_back(i);

int ancestor[26];
ranges::fill(ancestor, -1);
auto rebuild = [&](auto&& rebuild, int x) -> void {
int sx = s[x] - 'a';
int old = ancestor[sx];
ancestor[sx] = x;
for (int i = g[x].size() - 1; i >= 0; i--) {
int y = g[x][i];
int anc = ancestor[s[y] - 'a'];
if (anc != -1) {
g[anc].push_back(y);
g[x][i] = -1; // -1 表示删除 y
}
rebuild(rebuild, y);
}
ancestor[sx] = old; // 恢复现场
};
rebuild(rebuild, 0);

vector<int> size(n, 1); // 注意这里已经把 1 算进去了
auto dfs = [&](auto&& dfs, int x) -> void {
for (int y : g[x]) {
if (y != -1) { // y 没被删除
dfs(dfs, y);
size[x] += size[y];
}
}
};
dfs(dfs, 0);
return size;
}
};


class Solution {
public:
vector<int> findSubtreeSizes(vector<int>& parent, string s) {
int n = parent.size();
vector<vector<int>> g(n);
for (int i = 1; i < n; i++)
g[parent[i]].push_back(i);

vector<int> size(n, 1);
int ancestor[26];
ranges::fill(ancestor, -1);


auto dfs = [&](auto&& dfs, int x) -> void {
int sx = s[x] - 'a';
int old = ancestor[sx];
ancestor[sx] = x;
for (int y : g[x]) {
dfs(dfs, y);
int anc = ancestor[s[y] - 'a'];
size[anc < 0 ? x : anc] += size[y];
}
ancestor[sx] = old; // 恢复现场
};
dfs(dfs, 0);
return size;
}
};

http://www.gsyq.cn/news/148785.html

相关文章:

  • NX ①添加GC工具箱 ②制图绘制中心线 ③制图倒斜角标注C ④更新重量
  • 前端 | 一篇搞懂CSS盒模型核心:padding、margin、border与box-sizing、border-radius
  • 基于springboot服装商店管理与分析系统(毕设源码+文档)
  • 基于SpringBoot高校迎新管理系统(毕设源码+文档)
  • 当AI Agent学会“打电话“——微软Agent Framework的A2A与AGUI协议深度解析
  • fanxiudlg
  • YOLOv8改进 - 注意力机制 | SEAM (Spatially Enhanced Attention Module) 空间增强注意力模块提升遮挡目标特征学习能力
  • 事后诸葛分析
  • 模具设计 | UG软件官方正式版下载与安装教程指南
  • python+vue网约车在线打车拼车管理系统
  • 2025年AI搜索优化服务商实测榜单:平台覆盖与效果达标率对比 - 速递信息
  • AI进入心理咨询:效率跃升背后,隐私、偏见与“幻觉”如何兜底?
  • 百万医疗险推荐:2025 年用一份保单筑牢家庭健康防线 - 速递信息
  • 技术分享 / 客户 Demo 时,敏感数据防泄露的一种工程化方案
  • python+vue篮球人才球员管理系统vue
  • 2025.12.24
  • 2025最新!10个AI论文平台测评:本科生写论文还能这么快?
  • 博弈论小记(1)——基础博弈论概念
  • 2025继续教育必备9个降AI率工具测评榜单
  • 精选9款AI论文助手:高效完成开题报告与论文降重任务
  • Ubuntu 下配置 SFTP 服务并实现安全数据共享
  • AI辅助论文写作平台排名:9款工具实测,开题到降重全覆盖
  • 2025银川最新家电维修家政服务公司 TOP5 评测!兴庆、金凤、西夏、贺兰县等地区家庭生活服务团队权威榜单发布,专业高效解决家务难题 - 全局中转站
  • P9482 [NOI2023] 字符串
  • 大模型与传统AI的代际差异及大小协同的未来
  • AI论文写作工具测评:9款实测推荐,开题报告与降重功能全面解析
  • 工业质检多缺陷漏检,后来才知道融合X射线与热成像数据对齐特征
  • RS232 串口透传 IP 组网配置
  • Prodigy-HF 工具发布:NER训练与数据上传功能
  • 11574_springboot学生宿舍信息的系统(11574)