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

7.19 换根dp | vpp |滑窗

 

lcr147.最小栈

通过两个栈 维护实现

 class MinStack {
public:
stack<int> A, B;
MinStack() {}
void push(int x) {
A.push(x);
if(B.empty() || B.top() >= x)
B.push(x);
}
void pop() {
if(A.top() == B.top())
B.pop();
A.pop();
}
int top() {
return A.top();
}
int getMin() {
return B.top();

}
};

 

lcr180

特判,放在循环执行的前面

 

class Solution {
public:
vector<vector<int>> fileCombination(int target) {
int i = 1, j = 2, s = 3;
vector<vector<int>> res;
while(i < j) {
if(s == target) {
vector<int> ans;
for(int k = i; k <= j; k++)
ans.push_back(k);
res.push_back(ans);
}
if(s >= target) {
s -= i;
i++;
} else {
j++;
s += j;
}
}
return res;
}
};

 

lc973

数据结构vector<pair<double,pair<int,int>>>cnt

sort后取出k个

ans.push_back(vector<int>{cnt[i].second.first, cnt[i].second.second});

class Solution {
public:
double dist(vector<int>&a){
return sqrt(a[0] * a[0] + a[1] * a[1]);
}
vector<vector<int>> kClosest(vector<vector<int>>& points, int k)

{
vector<pair<double, pair<int,int>>>cnt;
for(int i = 0; i < points.size(); i++){
double d = dist(points[i]);
cnt.push_back({d, {points[i][0], points[i][1]}});
}
//按照坐标点进行升序排序
sort(cnt.begin(), cnt.end(), [](pair<double,pair<int,int>>&a,pair<double,pair<int,int>>&b)

      {
return a.first < b.first;
});
//记录答案
vector<vector<int>>ans;
for(int i = 0; i < k; i++)

      {
ans.push_back(vector<int>{cnt[i].second.first, cnt[i].second.second});
}
return ans;
}
};

 

 

 

换根dp 

最开始想到的是bfs的暴力遍历,也能归纳出数学关系优化

题解是由 父子关系-> 加减关系-dp tree

正解

详见oi-wiki dp tree

 

class Solution {
public:
vector<int> sumOfDistancesInTree(int n, vector<vector<int>>& edges) {
vector<vector<int>> g(n); // g[x] 表示 x 的所有邻居
for (auto& e: edges) {
int x = e[0], y = e[1];
g[x].push_back(y);
g[y].push_back(x);
}

        vector<int> ans(n);
vector<int> size(n, 1); // 注意这里初始化成 1 了,下面只需要累加儿子的子树大小
auto dfs = [&](auto&& dfs, int x, int fa, int depth) -> void {
ans[0] += depth; // depth 为 0 到 x 的距离
for (int y: g[x]) { // 遍历 x 的邻居 y
if (y != fa) { // 避免访问父节点
dfs(dfs, y, x, depth + 1); // x 是 y 的父节点
size[x] += size[y]; // 累加 x 的儿子 y 的子树大小
}
}
};
dfs(dfs, 0, -1, 0); // 0 没有父节点

        auto reroot = [&](auto&& reroot, int x, int fa) -> void {
for (int y: g[x]) { // 遍历 x 的邻居 y
if (y != fa) { // 避免访问父节点
ans[y] = ans[x] + n - 2 * size[y];
reroot(reroot, y, x); // x 是 y 的父节点
}
}
};
reroot(reroot, 0, -1); // 0 没有父节点
return ans;
}
};

 

 

数学归纳法

 


 

 

lc2944.选不选

遍历i,

for j  处理其影响范围,每步取最小的cache

o n^2,但是可以正向遍历,挺好想的

 class Solution {
public:
int minimumCoins(vector<int>& prices) {
int n = prices.size();
vector<int> dp(n + 1, 0x3f3f3f3f); 
dp[0] = 0;  

        for (int i = 1; i <= n; ++i) 

int end = min(2 * i, n); 

//处理其影响范围,每步取最小的cache
for (int j = i; j <= end; ++j)


dp[j] = min(dp[j],dp[i - 1] + prices[i - 1]); 

}
}

        return dp[n];
}
};

 

 

http://www.dtcms.com/a/287386.html

相关文章:

  • 医养照护与管理实训基地建设:创新模式与突破路径
  • 扭蛋机系统开发:打造多元化娱乐生态的新引擎
  • 响应式单位rpx及搭配使用UI产品工具
  • Ambiguity-Resolved Waveform Design for Cell-free OFDM-Based ISAC Systems
  • 【MySQL笔记】视图
  • 力扣 hot100 Day49
  • day25 力扣90.子集II 力扣46.全排列 力扣47.全排列 II
  • 二、环境搭建之CentOS安装Docker
  • GitHub:只支持 Git 作为唯一的版本库格式进行托管
  • 二分查找-69.x的平方根-力扣(LeetCode)
  • 安装单机版本Redis
  • 电商商品综合排序:从需求分析到实时计算的全方位指南
  • 初识Redis---Redis的特性介绍
  • GM-3568JHF vs 普通 RK3568 开发板 — 工业应用对比文案
  • 学习 Python 爬虫需要哪些基础知识?
  • 3516cv610 npu 开发典型功能点的介绍
  • Azure Bicep 是什么?
  • leetcode_121 买卖股票的最佳时期
  • AWS Partner: Accreditation (Technical)
  • 2025年渗透测试面试题总结-2025年HW(护网面试) 57(题目+回答)
  • QT动态加载动态库 QLibrary
  • 从车险理赔到快递签收:打通区块链与现实世界的“最后一公里”——解密预言机(Oracle)
  • aws(学习笔记第四十九课) ECS集中练习(1)
  • 基于深度学习的推荐系统:从协同过滤到神经网络
  • Java机考题:815. 公交路线 图论BFS
  • OpenCV 官翻7 - 对象检测
  • 用户中心——比如:腾讯的QQ账号可以登录到很多应用当中 02
  • Vue 3 中导出 Excel 文件
  • github上传代码
  • window、DOM、document、html 他们之间的关系是什么?