news 2026/4/18 8:03:53

vector<int> dfs

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
vector<int> dfs

lc3593

自底向上dfs

max dfs

cnt not need change子树

class Solution {
public:
int minIncrease(int n, vector<vector<int>>& edges, vector<int>& cost)

{
vector<vector<int>> g(n);
for (auto& e : edges) {
int x = e[0], y = e[1];
g[x].push_back(y);
g[y].push_back(x);
}
g[0].push_back(-1);

int ans = 0;
auto dfs = [&](this auto&& dfs, int x, int fa) -> long long {
long long max_s = 0;
int cnt = 0;
for (int y : g[x]) {
if (y == fa)
continue;

long long mx = dfs(y, x);
if (mx > max_s) {
max_s = mx;
cnt = 1;
}else if (mx == max_s)
cnt++;
}
ans += g[x].size() - 1 - cnt;//子树-fa-not need change

return max_s + cost[x];
};
dfs(0, -1);
return ans;
}
};

lc1519

vector<int> dfs

class Solution {
public:
vector<int> countSubTrees(int n, vector<vector<int>>& edges, string labels) {
vector<int> res(n, 0);
vector<vector<int>> g(n);
for(auto& e : edges) {
g[e[0]].push_back(e[1]);
g[e[1]].push_back(e[0]);
}
auto dfs = [&](this auto&& dfs,int u, int p)-> vector<int>
{
vector<int> cnt(26, 0);//cur
cnt[labels[u] - 'a']++;

for(int v : g[u]) {
if(v == p) continue;
vector<int> c = dfs(v, u);


for(int i = 0; i < 26; i++)
cnt[i] += c[i];
//拿到的每个v 都加给cur
}
res[u] = cnt[labels[u] - 'a'];
//统计完v后 存入ret

return cnt;
};
dfs(0, -1);// cur,fa
return res;
}
};

lc3254

统计连续上升的元素长度,用一次遍历判断每个长度为k的子数组是否连续上升 借助cnt变量

满足则记录末尾元素为能量值,否则保持-1

class Solution {
public:
vector<int> resultsArray(vector<int>& nums, int k) {
vector<int> ans(nums.size() - k + 1, -1);
int cnt = 0;
for (int i = 0; i < nums.size(); i++) {
cnt = i == 0 || nums[i] == nums[i - 1] + 1 ? cnt + 1 : 1;
if (cnt >= k)
ans[i - k + 1] = nums[i];
}
return ans;
}
};

lc957

模拟+周期+hash

模拟每天牢房状态变化+检测循环周期

高效计算出 n 天后的牢房状态,避免了大数 n 的重复模拟

class Solution {
public:
vector<int> prisonAfterNDays(vector<int>& cells, int n) {
unordered_map<int, vector<int>> map; //key:第i天 value:cells状态
map.insert({0, cells});


for (int i = 1; i <= n; i++) {
if (i == 1) {
cells[0] = 0;
cells[7] = 0;
}
for (int j = 1; j < 7; j++) {
if (map[i - 1][j - 1] == 0 && map[i - 1][j + 1] == 0) cells[j] = 1;
else if (map[i - 1][j - 1] == 1 && map[i - 1][j + 1] == 1) cells[j] = 1;
else cells[j] = 0;
}
map.insert({i, cells});
bool flag = true;// 判循环


for (int j = 0; j < 8; j++)
if (cells[j] != map[1][j]) flag = false;

if (i > 1 && flag) {
if (n % (i - 1) == 0) return map[i - 1]; //能整除则返回上一天的状态(余数为0)
else return map[n % (i - 1)];

//不能整除时返回第 n % (i - 1) 的状态
}
}
return cells;

//没出现循环直接返回模拟的结果
}
};

版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/4/18 5:41:09

7678678

687678687

作者头像 李华
网站建设 2026/4/18 5:24:20

Jupyter Notebook保存为Markdown格式:方便技术文档输出

Jupyter Notebook 保存为 Markdown&#xff1a;让实验成果轻松转化为技术文档 在深度学习项目中&#xff0c;我们常常会经历这样的场景&#xff1a;花了几天时间在 Jupyter Notebook 里调通模型、画出关键图表、写下分析逻辑&#xff0c;最后却卡在“怎么把这一切讲清楚”这一…

作者头像 李华
网站建设 2026/4/12 17:29:46

Conda环境备份迁移:复制现有PyTorch配置到新机器

Conda环境备份迁移&#xff1a;复制现有PyTorch配置到新机器 在深度学习项目中&#xff0c;最让人头疼的往往不是模型调参&#xff0c;而是“在我电脑上明明能跑”的环境问题。一个团队里五个人装环境&#xff0c;最后可能配出三种不同的行为结果——有人CUDA不识别&#xff0c…

作者头像 李华
网站建设 2026/4/15 23:39:12

Conda列出已安装包:筛选出与PyTorch相关的库

Conda筛选PyTorch相关包&#xff1a;高效验证深度学习环境完整性的实践指南 在深度学习项目中&#xff0c;最令人沮丧的场景之一莫过于代码写完准备训练时&#xff0c;却突然报出 ModuleNotFoundError: No module named torch。更糟的是&#xff0c;在远程服务器或团队共享环境…

作者头像 李华
网站建设 2026/4/18 5:42:30

Conda环境删除恢复:误删后如何找回PyTorch配置

Conda环境删除恢复&#xff1a;误删后如何找回PyTorch配置 在深度学习项目开发中&#xff0c;一个稳定的运行环境往往比代码本身更“脆弱”。你可能花了一整天调试好 PyTorch CUDA 的版本组合&#xff0c;结果一条 conda remove -n pytorch_env --all 命令误执行&#xff0c;…

作者头像 李华
网站建设 2026/4/4 1:44:41

Conda环境变量设置:指定CUDA_VISIBLE_DEVICES控制GPU使用

Conda环境变量设置&#xff1a;指定CUDA_VISIBLE_DEVICES控制GPU使用 在现代深度学习开发中&#xff0c;我们经常面对这样一个现实&#xff1a;服务器上插着四块A100显卡&#xff0c;但你只想用其中一块跑实验&#xff0c;而同事正占用另一张卡训练大模型。如果程序一启动就抢占…

作者头像 李华