当前位置:网站首页>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];
}
};
边栏推荐
- 【视频】马尔可夫链蒙特卡罗方法MCMC原理与R语言实现|数据分享
- [rust web rokcet Series 2] connect the database and add, delete, modify and check curd
- Automatically browse pinduoduo products
- This is the report that leaders like! Learn dynamic visual charts, promotion and salary increase are indispensable
- Electronic Society C language level 1 32, calculate the power of 2
- 1218 square or round
- Parted command
- JMeter (II) - install the custom thread groups plug-in
- 734. Energy stone (greed, backpack)
- The concepts and differences between MySQL stored procedures and stored functions, as well as how to create them, the role of delimiter, the viewing, modification, deletion of stored procedures and fu
猜你喜欢

MySQL主从延迟问题怎么解决

Feature extraction and detection 16 brisk feature detection and matching

Unity AssetBundle subcontracting

人工智能在网络安全中的作用

Number of palindromes in C language (leetcode)
![[技术发展-21]:网络与通信技术的应用与发展快速概览-1- 互联网网络技术](/img/2d/299fa5c76416f74bd1a693c433dd09.png)
[技术发展-21]:网络与通信技术的应用与发展快速概览-1- 互联网网络技术

Basic concepts of machine learning

Learn about servlets

Android high frequency network interview topic must know and be able to compare Android development environment

Six lessons to be learned for the successful implementation of edge coding
随机推荐
What are the affordable Bluetooth headsets? Student party parity Bluetooth headset recommendation
10 minutes to get started quickly composition API (setup syntax sugar writing method)
卷積神經網絡(包含代碼與相應圖解)
医药管理系统(大一下C语言课设)
Laravel artisan common commands
The concept, function, characteristics, creation and deletion of MySQL constraints
Réseau neuronal convolutif (y compris le Code et l'illustration correspondante)
Architecture evolution from MVC to DDD
734. Energy stone (greed, backpack)
np. Where and torch Where usage
Convolutional neural network (including code and corresponding diagram)
[C #] use regular verification content
matlab 使用 audiorecorder、recordblocking录制声音,play 播放声音,audiowrite 保存声音
How to debug apps remotely and online?
uTools
SAP ui5 beginner tutorial 20 - explanation of expression binding usage of SAP ui5
MATLAB realizes voice signal resampling and normalization, and plays the comparison effect
Ks006 student achievement management system based on SSM
[Floyd] post disaster reconstruction
成功实现边缘编码需要了解的六大经验教训