当前位置:网站首页>leetcode2305. 公平分发饼干(中等,周赛,状压dp)
leetcode2305. 公平分发饼干(中等,周赛,状压dp)
2022-07-02 01:51:00 【重you小垃】
思路一:暴力dfs,时间复杂度为O(n^n),时间复杂度有些高。
思路二:子集划分+子集个数有限制->状压dp 时间复杂度O(n * 3^n)是固定的
状压dp相关知识:
// 遍历u的所有非空子集
for (int s = u; s; s = (s - 1) & u) {
//s表示u的一个子集 u^s表示去掉s后的子集
}
具体思路:
dp[i][j]表示消耗的集合个数为i,选择的饼干为j所获得饼干最大总数的最小值。
i的取值为1->k j的取值为1->(1<<n)
dp[i][j]=min(max(dp[i-1][j^s], sum[s]));
class Solution {
public:
int distributeCookies(vector<int>& cookies, int k) {
int n = cookies.size();
vector<int> sum(1 << n);
for (int i = 1; i < (1 << n); ++i) {
for (int j = 0; j < n; ++j) {
if (i & (1 << j)) sum[i] += cookies[j];
}
}
vector<vector<int>> dp(k, vector<int>(1 << n));
//dp[0]表示消耗1组,所获得的值
dp[0] = sum;
for (int i = 1; i < k; ++i) {
for (int j = 1; j < (1 << n); ++j) {
dp[i][j] = INT_MAX;
for (int s = j; s; s = (s - 1) & j) {
dp[i][j] = min(dp[i][j], max(dp[i - 1][j ^ s], sum[s]));
}
}
}
return dp[k - 1][(1 << n) - 1];
}
};
边栏推荐
- Experimental reproduction of variable image compression with a scale hyperprior
- leetcode2311. 小于等于 K 的最长二进制子序列(中等,周赛)
- JMeter (II) - install the custom thread groups plug-in
- C language 3-7 daffodils (enhanced version)
- 大学的知识是否学而无用、过时?
- Should enterprises choose server free computing?
- matlab 使用 resample 完成重采样
- Private project practice sharing [Yugong series] February 2022 U3D full stack class 009 unity object creation
- [技术发展-21]:网络与通信技术的应用与发展快速概览-1- 互联网网络技术
- leetcode2310. 个位数字为 K 的整数之和(中等,周赛)
猜你喜欢
Ks006 student achievement management system based on SSM
现货黄金分析的技巧有什么呢?
Learn basic K-line diagram knowledge in three minutes
Six lessons to be learned for the successful implementation of edge coding
321. Chessboard segmentation (2D interval DP)
It's already 30. Can you learn programming from scratch?
卷积神经网络(包含代码与相应图解)
成功实现边缘编码需要了解的六大经验教训
SAP ui5 beginner tutorial 20 - explanation of expression binding usage of SAP ui5
遊戲思考15:全區全服和分區分服的思考
随机推荐
JMeter (II) - install the custom thread groups plug-in
Exception handling of class C in yyds dry goods inventory
Architecture evolution from MVC to DDD
Six lessons to be learned for the successful implementation of edge coding
I Brief introduction of radio energy transmission technology
KS006基于SSM实现学生成绩管理系统
2022 Q2 - 提昇技能的技巧總結
JMeter (I) - download, installation and plug-in management
479. Additive binary tree (interval DP on the tree)
1222. Password dropping (interval DP, bracket matching)
C language 3-7 daffodils (enhanced version)
[Obsidian] wechat is sent to Obsidian using remotely save S3 compatibility
城市选择器组件实现原理
自动浏览拼多多商品
成功实现边缘编码需要了解的六大经验教训
三分钟学会基础k线图知识
卷积神经网络(包含代码与相应图解)
uTools
matlab 使用 audiorecorder、recordblocking录制声音,play 播放声音,audiowrite 保存声音
matlab 使用 resample 完成重采样