当前位置:网站首页>LeetCode每日一题(2305. Fair Distribution of Cookies)
LeetCode每日一题(2305. Fair Distribution of Cookies)
2022-07-03 09:01:00 【wangjun861205】
You are given an integer array cookies, where cookies[i] denotes the number of cookies in the ith bag. You are also given an integer k that denotes the number of children to distribute all the bags of cookies to. All the cookies in the same bag must go to the same child and cannot be split up.
The unfairness of a distribution is defined as the maximum total cookies obtained by a single child in the distribution.
Return the minimum unfairness of all distributions.
Example 1:
Input: cookies = [8,15,10,20,8], k = 2
Output: 31
Explanation: One optimal distribution is [8,15,8] and [10,20]
- The 1st child receives [8,15,8] which has a total of 8 + 15 + 8 = 31 cookies.
- The 2nd child receives [10,20] which has a total of 10 + 20 = 30 cookies.
The unfairness of the distribution is max(31,30) = 31.
It can be shown that there is no distribution with an unfairness less than 31.
Example 2:
Input: cookies = [6,1,3,2,2,4,1,2], k = 3
Output: 7
Explanation: One optimal distribution is [6,1], [3,2,2], and [4,1,2]
- The 1st child receives [6,1] which has a total of 6 + 1 = 7 cookies.
- The 2nd child receives [3,2,2] which has a total of 3 + 2 + 2 = 7 cookies.
- The 3rd child receives [4,1,2] which has a total of 4 + 1 + 2 = 7 cookies.
The unfairness of the distribution is max(7,7,7) = 7.
It can be shown that there is no distribution with an unfairness less than 7.
Constraints:
- 2 <= cookies.length <= 8
- 1 <= cookies[i] <= 105
- 2 <= k <= cookies.length
站在饼干的角度来看, 每一袋饼干必定要属于某一个孩子, 我们把每种分配情况都枚举出来取最小的最大不公平度。
note: 切身体会到内存分配不仅耗内存空间,也耗时间(貌似是废话,但实测时间消耗确实有点大),同样的方法,我采用的办法是把当前的状态数组拷贝一份新的然后修改传入下一层, 得到的结果就是超时, 但是用他们说的 backtracking 的方法,传递引用就可以顺利通过
impl Solution {
fn backtracking(cookies: &Vec<i32>, k: i32, i: usize, stat: &mut Vec<i32>) -> i32 {
if i == cookies.len() {
return *stat.iter().max().unwrap();
}
let mut ans = i32::MAX;
for n in 0..k as usize {
stat[n] += cookies[i];
ans = ans.min(Solution::backtracking(cookies, k, i + 1, stat));
stat[n] -= cookies[i];
}
ans
}
pub fn distribute_cookies(cookies: Vec<i32>, k: i32) -> i32 {
Solution::backtracking(&cookies, k, 0, &mut vec![0; k as usize])
}
}
边栏推荐
- Beego learning - Tencent cloud upload pictures
- Pic16f648a-e/ss PIC16 8-bit microcontroller, 7KB (4kx14)
- [solution to the new version of Flink without bat startup file]
- Move anaconda, pycharm and jupyter notebook to mobile hard disk
- [graduation season | advanced technology Er] another graduation season, I change my career as soon as I graduate, from animal science to programmer. Programmers have something to say in 10 years
- LeetCode每日一题(516. Longest Palindromic Subsequence)
- What are the stages of traditional enterprise digital transformation?
- Save the drama shortage, programmers' favorite high-score American drama TOP10
- Flink学习笔记(十)Flink容错机制
- How to check whether the disk is in guid format (GPT) or MBR format? Judge whether UEFI mode starts or legacy mode starts?
猜你喜欢
Hudi 集成 Spark 数据分析示例(含代码流程与测试结果)
【Kotlin学习】高阶函数的控制流——lambda的返回语句和匿名函数
State compression DP acwing 91 Shortest Hamilton path
[point cloud processing paper crazy reading classic version 14] - dynamic graph CNN for learning on point clouds
Hudi学习笔记(三) 核心概念剖析
CATIA automation object architecture - detailed explanation of application objects (III) systemservice
【点云处理之论文狂读经典版14】—— Dynamic Graph CNN for Learning on Point Clouds
[point cloud processing paper crazy reading classic version 11] - mining point cloud local structures by kernel correlation and graph pooling
[set theory] order relation (eight special elements in partial order relation | ① maximum element | ② minimum element | ③ maximum element | ④ minimum element | ⑤ upper bound | ⑥ lower bound | ⑦ minimu
Vscode编辑器右键没有Open In Default Browser选项
随机推荐
STM32F103 can learning record
Computing level network notes
[graduation season | advanced technology Er] another graduation season, I change my career as soon as I graduate, from animal science to programmer. Programmers have something to say in 10 years
Temper cattle ranking problem
Install database -linux-5.7
Derivation of Fourier transform
Data mining 2021-4-27 class notes
[set theory] order relation (eight special elements in partial order relation | ① maximum element | ② minimum element | ③ maximum element | ④ minimum element | ⑤ upper bound | ⑥ lower bound | ⑦ minimu
【点云处理之论文狂读经典版7】—— Dynamic Edge-Conditioned Filters in Convolutional Neural Networks on Graphs
The less successful implementation and lessons of RESNET
Recommend a low code open source project of yyds
MySQL installation and configuration (command line version)
2022-2-14 learning xiangniuke project - Session Management
Flink学习笔记(九)状态编程
Integrated use of interlij idea and sonarqube
Vscode编辑器右键没有Open In Default Browser选项
Banner - Summary of closed group meeting
Crawler career from scratch (IV): climb the bullet curtain of station B through API
CATIA automation object architecture - detailed explanation of application objects (III) systemservice
【点云处理之论文狂读经典版8】—— O-CNN: Octree-based Convolutional Neural Networks for 3D Shape Analysis