预统计

lc826

++// 记得预处理:维护每个难度对应的最大收益
for(int i=1;i<n;i++){
dpi.second = max(dpi.second, dpi-1.second);
++

class Solution {

public:

int maxProfitAssignment(vector<int>& difficulty, vector<int>& profit, vector<int>& worker)

{

vector<pair<int,int>> dp;

int m=worker.size(),n=difficulty.size();

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

{

dp.push_back({difficultyi,profiti});

}

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

++// 预处理:维护每个难度对应的最大收益
for(int i=1;i<n;i++){
dpi.second = max(dpi.second, dpi-1.second);
++

}

int ret=0;

for(int i=0;i<m;i++)

{

int t=upper_bound(dp.begin(),dp.end(),make_pair(workeri, INT_MAX))-dp.begin()-1;

if(t>=0) ret+=dpt.second;

}

return ret;

}

};

lcr49

++if(!node->left && !node->right)
{
int t=stoi(path);
ret+=t;
}
else
++

class Solution {

public:

int sumNumbers(TreeNode* root)

{

string path;

int ret=0;

auto dfs=\&(this auto&& dfs,TreeNode* node)

{

if(!node)

return;

path+=(node->val+'0');

++if(!node->left && !node->right)
{
int t=stoi(path);
ret+=t;
}
else
++

{

dfs(node->left);

dfs(node->right);

}

path.pop_back();

};

dfs(root);

return ret;

}

};

lc1457

mask优化回溯

init 0

  • ^(1<<i) 实现标记

__builtin_popcount(mask) <= 1 实现统计

class Solution {

public:

int pseudoPalindromicPaths (TreeNode* root)

{

int mask = 0, ret = 0; // mask用二进制位记录数字出现次数的奇偶

auto dfs=\&(this auto&& dfs,TreeNode* node) {

if(!node) return;

mask ^= (1 << node->val); // 异或:出现次数奇偶翻转

if(!node->left && !node->right) {

// 伪回文:mask中1的数量≤1

ret += (__builtin_popcount(mask) <= 1);

} else {

dfs(node->left);

dfs(node->right);

}

mask ^= (1 << node->val); // 回溯

};

dfs(root);

return ret;

}

};

string 和 unordered_map 效率低了tle了..

++// 仅叶子节点检查路径++

++if(!node->left && !node->right)++

++if(check(path)) ret++;++

class Solution {

public:

int pseudoPalindromicPaths (TreeNode* root)

{

string path;

int ret=0;

auto check=\&(string s) {

unordered_map<char,int> hash;

for(auto& c:s) hashc++;

bool f=false;

for(auto& _,b:hash)

{

if(b%2==1)

{

if(!f) f=true;

else return false;

}

}

return true;

};

auto dfs=\&(this auto&& dfs,TreeNode* node) {

if(!node) return;

path.push_back(node->val+'0');

++// 仅叶子节点检查路径++

++if(!node->left && !node->right) {++

++if(check(path)) ret++;++

}

else {

dfs(node->left);

dfs(node->right);

}

path.pop_back();//回溯

};

dfs(root);

return ret;

}

};

lc1267

预处理统计行列

class Solution {

public:

int countServers(vector<vector<int>>& grid)

{

int m = grid.size(), n = grid0.size();

vector<int> row(m, 0), col(n, 0);

// 先统计每行、每列的服务器数量

for (int i = 0; i < m; i++) {

for (int j = 0; j < n; j++) {

if (gridij == 1) {

rowi++;

colj++;

}

}

}

int ret = 0;

// 遍历每个服务器,判断其行/列是否有至少2个服务器

for (int i = 0; i < m; i++) {

for (int j = 0; j < n; j++) {

if (gridij == 1 && (rowi > 1 || colj > 1)) {

ret++;

}

}

}

return ret;

}

};

相关推荐
逆境不可逃3 小时前
【LeetCode 912】排序数组——随机化快速排序详解
数据结构·算法·排序算法
Coder-magician3 小时前
《代码随想录》刷题打卡day31:动态规划-背包问题part02
算法·动态规划
薛定e的猫咪3 小时前
从因果视角解决多智能体协作:细读 SCIC 算法
算法
c238563 小时前
《算法武林谱:四大排序神功与二分寻宝术全解》
数据结构·算法·排序算法
一米阳光86613 小时前
软考(中级)软件设计师核心笔记(9)算法——时间复杂度与空间复杂度、查找算法、排序算法
笔记·算法·职场发展·软考·软件设计师·中级职称
Mem0rin3 小时前
二分查找:左右边界
数据结构·算法
大明者省13 小时前
WSL2 Ubuntu22.04 GPU训练环境配置指南
人工智能·算法·计算机视觉
白狐_79815 小时前
408数据结构第8章:排序②——性质对比秒杀、场景选择与外部排序
java·数据结构·算法
zander25815 小时前
LeetCode 84:柱状图中的最大矩形——单调栈如何确定左右边界
java·数据结构·算法
Shell运维手记15 小时前
Linux 常用基础命令学习笔记
linux·运维·笔记·学习·算法·github