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

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;

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

http://www.zskr.cn/news/177227.html

相关文章:

  • 如何查看GPU显存占用?nvidia-smi与PyTorch监控结合使用
  • JiyuTrainer支持Hyperparameter Sweep:自动搜索最优配置
  • PyTorch DataLoader多线程加载数据:提升GPU利用率
  • Conda环境克隆:快速复制已验证的PyTorch配置
  • 强化学习笔记
  • 从零搭建深度学习工作站:选择合适的CUDA驱动与PyTorch版本
  • diskinfo工具监测SSD寿命:保障GPU服务器稳定运行
  • Anaconda配置PyTorch环境最佳实践:含CUDA版本匹配技巧
  • 开源FOC平衡车固件:重新定义电动平衡车控制体验
  • GitHub Template仓库快速生成PyTorch-CUDA项目结构
  • 省选集训 4 - 图论与网络流
  • PyTorch安装教程GPU版:CentOS系统适配指南
  • MySQL数据库 - 努力-
  • GitHub仓库结构设计:组织PyTorch项目代码的最佳方式
  • 【飞书入门】1-飞书支持Markdown 吗
  • 马头是区——团队总结
  • PyTorch-CUDA-v2.8镜像日志轮转策略防止磁盘占满
  • MCP Inspector中Streamable HTTP授权头缺失问题的技术诊断与解决方案
  • 软工学期总结
  • HTML 媒体(Media)
  • js-原型链检测
  • Conda环境变量设置技巧:优化PyTorch运行行为
  • PyTorch-CUDA-v2.8镜像未来更新路线图展望
  • PyTorch-CUDA-v2.8镜像体积优化:精简不必要的依赖包
  • Git Hooks自动化检查PyTorch代码提交规范
  • Java毕设选题推荐:基于springBoot的高校毕业生公职资讯系统的设计与实现资讯聚合 - 报考匹配 - 资源管理 - 互动交流” 一体化平【附源码、mysql、文档、调试+代码讲解+全bao等】
  • Conda环境备份与恢复:保障PyTorch项目连续性
  • PyTorch-CUDA-v2.8镜像用户反馈收集渠道建设
  • PyTorch-CUDA-v2.8镜像安全加固措施清单
  • Conda环境隔离原则:避免PyTorch依赖污染