当前位置:网站首页>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])
}
}
边栏推荐
- Move anaconda, pycharm and jupyter notebook to mobile hard disk
- LeetCode 715. Range module
- [kotlin learning] control flow of higher-order functions -- lambda return statements and anonymous functions
- [point cloud processing paper crazy reading classic version 9] - pointwise revolutionary neural networks
- Just graduate student reading thesis
- Flink学习笔记(十)Flink容错机制
- Go language - IO project
- 【点云处理之论文狂读经典版11】—— Mining Point Cloud Local Structures by Kernel Correlation and Graph Pooling
- Crawler career from scratch (II): crawl the photos of my little sister ② (the website has been disabled)
- Temper cattle ranking problem
猜你喜欢
Vs2019 configuration opencv3 detailed graphic tutorial and implementation of test code
Digital statistics DP acwing 338 Counting problem
[point cloud processing paper crazy reading frontier version 10] - mvtn: multi view transformation network for 3D shape recognition
MySQL installation and configuration (command line version)
Overview of image restoration methods -- paper notes
Spark 集群安装与部署
【点云处理之论文狂读前沿版10】—— MVTN: Multi-View Transformation Network for 3D Shape Recognition
LeetCode 715. Range module
Hudi integrated spark data analysis example (including code flow and test results)
Idea uses the MVN command to package and report an error, which is not available
随机推荐
WARNING: You are using pip version 21.3.1; however, version 22.0.3 is available. Prompt to upgrade pip
Hudi学习笔记(三) 核心概念剖析
Severity code description the project file line prohibits the display of status error c2440 "initialization": unable to convert from "const char [31]" to "char *"
[point cloud processing paper crazy reading classic version 9] - pointwise revolutionary neural networks
【点云处理之论文狂读经典版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
Overview of image restoration methods -- paper notes
Using Hudi in idea
LeetCode 57. Insert interval
Common formulas of probability theory
Simple use of MATLAB
LeetCode每日一题(1300. Sum of Mutated Array Closest to Target)
Navicat, MySQL export Er graph, er graph
Hudi 快速体验使用(含操作详细步骤及截图)
Explanation of the answers to the three questions
The "booster" of traditional office mode, Building OA office system, was so simple!
Flink学习笔记(八)多流转换
【点云处理之论文狂读经典版10】—— PointCNN: Convolution On X-Transformed Points
Jenkins learning (II) -- setting up Chinese
[kotlin learning] operator overloading and other conventions -- overloading the conventions of arithmetic operators, comparison operators, sets and intervals