预统计

lc826

++// 记得预处理:维护每个难度对应的最大收益
for(int i=1;i<n;i++){
dp[i].second = max(dp[i].second, dp[i-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({difficulty[i],profit[i]});

}

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

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

}

int ret=0;

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

{

int t=upper_bound(dp.begin(),dp.end(),make_pair(worker[i], INT_MAX))-dp.begin()-1;

if(t>=0) ret+=dp[t].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) hash[c]++;

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 = grid[0].size();

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

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

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

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

if (grid[i][j] == 1) {

row[i]++;

col[j]++;

}

}

}

int ret = 0;

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

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

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

if (grid[i][j] == 1 && (row[i] > 1 || col[j] > 1)) {

ret++;

}

}

}

return ret;

}

};

相关推荐
范纹杉想快点毕业11 小时前
欧几里得算法与扩展欧几里得算法,C语言编程实现(零基础全解析)
运维·c语言·单片机·嵌入式硬件·算法
f***241111 小时前
Bug悬案:技术侦探的破案指南
算法·bug
Swift社区11 小时前
LeetCode 472 连接词
算法·leetcode·职场和发展
CoovallyAIHub11 小时前
YOLO-Maste开源:首个MoE加速加速实时检测,推理提速17.8%!
深度学习·算法·计算机视觉
清铎11 小时前
leetcode_day13_普通数组_《绝境求生》
数据结构·算法
hetao173383711 小时前
2026-01-09~12 hetao1733837 的刷题笔记
c++·笔记·算法
过河卒_zh156676612 小时前
情感型AI被“立规矩”,AI陪伴时代进入下半场
人工智能·算法·aigc·生成式人工智能·算法备案
wefg112 小时前
【算法】动态规划
算法·动态规划
机器学习之心12 小时前
198种组合算法+优化TCN-Transformer+SHAP分析+新数据预测+多输出!深度学习可解释分析,强烈安利,粉丝必备!
深度学习·算法·transformer·shap分析·新数据预测
狐5712 小时前
2026-01-12-LeetCode刷题笔记-1266-访问所有点的最小时间.md
笔记·算法·leetcode