7.19 换根dp | vpp |滑窗

lc1619.去掉前后5%

class Solution {

public:

double trimMean(vector<int>& arr) {

int len = arr.size() * 0.05;

sort(arr.begin(), arr.end());

double sum = 0;

++for(int i = len; i < arr.size() - len; i++){++

sum += arri;

}

return sum / (arr.size() - 2 * len);

}

};

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>{cnti.second.first, cnti.second.second}++);

class Solution {

public:

double dist(vector<int>&a){

return sqrt(a0 * a0 + a1 * a1);

}

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(pointsi);

cnt.push_back({d, {pointsi0, pointsi1}});

}

//按照坐标点进行升序排序

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>{cnti.second.first, cnti.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); // gx 表示 x 的所有邻居

for (auto& e: edges) {

int x = e0, y = e1;

gx.push_back(y);

gy.push_back(x);

}

vector<int> ans(n);

vector<int> size(n, 1); // 注意这里初始化成 1 了,下面只需要累加儿子的子树大小

auto dfs = \&(auto&& dfs, int x, int fa, int depth) -> void {

ans0 += depth; // depth 为 0 到 x 的距离

for (int y: gx) { // 遍历 x 的邻居 y

if (y != fa) { // 避免访问父节点

dfs(dfs, y, x, depth + 1); // x 是 y 的父节点

sizex += sizey; // 累加 x 的儿子 y 的子树大小

}

}

};

dfs(dfs, 0, -1, 0); // 0 没有父节点

auto reroot = \&(auto&& reroot, int x, int fa) -> void {

for (int y: gx) { // 遍历 x 的邻居 y

if (y != fa) { // 避免访问父节点

ansy = ansx + n - 2 * sizey;

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);

dp0 = 0;

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

{

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

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

++{
dpj = min(dpj,dpi - 1 + pricesi - 1);
++

}

}

return dpn;

}

};

相关推荐
CoderYanger10 小时前
A.每日一题:1140. 石子游戏 II
java·程序人生·算法·leetcode·游戏·职场和发展·深度优先
小雨笙笙10 小时前
机器学习:评估模型与选择
人工智能·算法·机器学习
高亦真10 小时前
今天是学习嵌入式的第35天
linux·学习·算法
mengzhi啊10 小时前
QEventLoop loop配合loop.exec();替代while(1)。电脑cpu占用为0,使用while(1)cpu占用率100%
c++·qt
HugoStudio_SWAN10 小时前
洛谷 P1319 / P1320 压缩技术——同一枚硬币的正反面
c++·学习·程序人生·算法
小小晓.11 小时前
C++单例模式万字详解:6种实现演进+线程安全彻底解决+CRTP终极模板
c++·单例模式
j7~12 小时前
【C++11】C++11 新特性全套详解(列表初始化、右值引用、lambda 表达式、function 与 bind 包装器等)
c++·右值引用·lambda表达式·可变参数模板·移动语义·包装器·c++11发展史
wdfk_prog12 小时前
canopennode-rtt推荐,不只可以做从站,也可以承担主站角色
c语言·开发语言·数据库·学习·算法·深度优先
无小道12 小时前
C/C++——可变参数
c语言·开发语言·c++·可变参数
BioRunYiXue12 小时前
科研干货 | IC50全面解读:概念解析、实验设计与数据分析要点
java·开发语言·javascript·人工智能·算法·数据挖掘·数据分析