当前位置:网站首页>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];
}
};
边栏推荐
- Based on configured schedule, the given trigger will never fire
- Feature extraction and detection 16 brisk feature detection and matching
- [Video] Markov chain Monte Carlo method MCMC principle and R language implementation | data sharing
- The difference between new and malloc
- From January 11, 2007 to January 11, 2022, I have been in SAP Chengdu Research Institute for 15 years
- JMeter (II) - install the custom thread groups plug-in
- Based on configured schedule, the given trigger will never fire
- 【视频】马尔可夫链原理可视化解释与R语言区制转换MRS实例|数据分享
- 微信小程序中使用tabBar
- 基于SSM实现微博系统
猜你喜欢

Game thinking 15: thinking about the whole region and sub region Services

TSINGSEE青犀平台如何实现同一节点同时播放多个视频?

MySQL约束与多表查询实例分析

SQLite 3 of embedded database

Data analysis on the disaster of Titanic

Using tabbar in wechat applet

Five skills of adding audio codec to embedded system

MATLAB realizes voice signal resampling and normalization, and plays the comparison effect

ES6 new method of string

跨域?同源?一次搞懂什么是跨域
随机推荐
Cross domain? Homology? Understand what is cross domain at once
Five skills of adding audio codec to embedded system
Data analysis on the disaster of Titanic
ES6 new method of string
Unity AssetBundle subcontracting
1218 square or round
Four basic strategies for migrating cloud computing workloads
大学的知识是否学而无用、过时?
[Obsidian] wechat is sent to Obsidian using remotely save S3 compatibility
Convolutional neural network (including code and corresponding diagram)
微信小程序中使用tabBar
Android: the kotlin language uses grendao3, a cross platform app development framework
matlab 使用 audioread 、 sound 读取和播放 wav 文件
跨域?同源?一次搞懂什么是跨域
卷積神經網絡(包含代碼與相應圖解)
三分钟学会基础k线图知识
2022 Q2 - 提昇技能的技巧總結
VARIATIONAL IMAGE COMPRESSION WITH A SCALE HYPERPRIOR文献实验复现
6-2 vulnerability exploitation - inevitable problems of FTP
The role of artificial intelligence in network security