当前位置:网站首页>leetcode2305. Fair distribution of biscuits (medium, weekly, shaped pressure DP)
leetcode2305. Fair distribution of biscuits (medium, weekly, shaped pressure DP)
2022-07-02 01:53:00 【Heavy garbage】
Train of thought : violence dfs, The time complexity is O(n^n), The time complexity is a little high .
Train of thought two : subset division + The number of subsets is limited -> Pressure dp Time complexity O(n * 3^n) Is constant
Pressure dp Related knowledge :
// Traverse u All nonempty subsets of
for (int s = u; s; s = (s - 1) & u) {
//s Express u A subset of u^s Means to remove s A subset of the latter
}
Specific ideas :
dp[i][j] Indicates that the number of sets consumed is i, The selected cookies are j The minimum value of the maximum total number of cookies obtained .
i The values for 1->k j The values for 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] It means consumption 1 Group , The value obtained
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];
}
};
边栏推荐
- 734. Energy stone (greed, backpack)
- How can the tsingsee Qingxi platform play multiple videos at the same time on the same node?
- Failed to transform file 'xxx' to match attributes
- 2022 Q2 - 提昇技能的技巧總結
- II Basic structure of radio energy transmission system
- [Floyd] post disaster reconstruction
- Ks006 student achievement management system based on SSM
- Construction and maintenance of business websites [10]
- MySQL中一条SQL是怎么执行的
- 自动浏览拼多多商品
猜你喜欢
【LeetCode 43】236. The nearest common ancestor of binary tree
Software No.1
Feature extraction and detection 16 brisk feature detection and matching
城市选择器组件实现原理
Game thinking 15: thinking about the whole region and sub region Services
leetcode373. 查找和最小的 K 对数字(中等)
The technology boss is ready, and the topic of position C is up to you
Medical management system (C language course for freshmen)
k线图形态这样记(口诀篇)
Réseau neuronal convolutif (y compris le Code et l'illustration correspondante)
随机推荐
The role of artificial intelligence in network security
Réseau neuronal convolutif (y compris le Code et l'illustration correspondante)
Construction and maintenance of business websites [12]
Altium designer measure distance (ctrl+m)
[Video] Markov chain Monte Carlo method MCMC principle and R language implementation | data sharing
跨域?同源?一次搞懂什么是跨域
It's already 30. Can you learn programming from scratch?
What are the skills of spot gold analysis?
Raspberry pie 4B learning notes - IO communication (1-wire)
Matlab uses resample to complete resampling
new和malloc的区别
Pyldavis installation and use | attributeerror: module 'pyldavis' has no attribute' gensim '| visual results are exported as separate web pages
Openssl3.0 learning XXI provider encoder
Automatically browse pinduoduo products
Have you stepped on the nine common pits in the e-commerce system?
k线图形态这样记(口诀篇)
Implementation of Weibo system based on SSM
Niuke - Huawei question bank (51~60)
Five skills of adding audio codec to embedded system
Android: how can golden nine and silver ten squeeze into the first-line big factories from small and medium-sized enterprises? The depth of interview questions in large factories