当前位置:网站首页>Leetcode daily question (2305. fair distribution of cookies)
Leetcode daily question (2305. fair distribution of cookies)
2022-07-03 09:33: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
From the perspective of cookies , Every bag of biscuits must belong to a child , We enumerate each distribution to take the minimum and maximum inequality .
note: Personally realize that memory allocation not only consumes memory space , It also takes time ( It seems like bullshit , But the measured time consumption is really a little big ), Same method , My method is to copy a new copy of the current state array, and then modify it and transfer it to the next layer , The result is timeout , But as they say backtracking Methods , Pass the reference and it will pass smoothly
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])
}
}
边栏推荐
- [point cloud processing paper crazy reading cutting-edge version 12] - adaptive graph revolution for point cloud analysis
- Django operates Excel files through openpyxl to import data into the database in batches.
- Crawler career from scratch (I): crawl the photos of my little sister ① (the website has been disabled)
- Spark cluster installation and deployment
- LeetCode每日一题(2109. Adding Spaces to a String)
- PIP configuring domestic sources
- Crawler career from scratch (II): crawl the photos of my little sister ② (the website has been disabled)
- Spark overview
- Modify idea code
- Database execution error: SQL_ mode only_ full_ group_ by:
猜你喜欢
[CSDN]C1训练题解析_第三部分_JS基础
npm install安装依赖包报错解决方法
NPM install installation dependency package error reporting solution
Hudi data management and storage overview
Principles of computer composition - cache, connection mapping, learning experience
Construction of simple database learning environment
PolyWorks script development learning notes (4) - data import and alignment using file import
2022-2-14 learning xiangniuke project - Session Management
【Kotlin学习】类、对象和接口——定义类继承结构
Windows安装Redis详细步骤
随机推荐
Flask+supervisor installation realizes background process resident
The server denied password root remote connection access
Logstash+jdbc data synchronization +head display problems
LeetCode每日一题(985. Sum of Even Numbers After Queries)
PolyWorks script development learning notes (III) -treeview advanced operation
软件测试工程师是做什么的 通过技术测试软件程序中是否有漏洞
What do software test engineers do? Pass the technology to test whether there are loopholes in the software program
Solve POM in idea Comment top line problem in XML file
[CSDN]C1训练题解析_第三部分_JS基础
WARNING: You are using pip ; however. Later, upgrade PIP failed, modulenotfounderror: no module named 'pip‘
Explanation of the answers to the three questions
Flink学习笔记(八)多流转换
[kotlin learning] classes, objects and interfaces - define class inheritance structure
[solution to the new version of Flink without bat startup file]
图像修复方法研究综述----论文笔记
Spark 概述
LeetCode每日一题(931. Minimum Falling Path Sum)
小王叔叔的博客目录【持续更新中】
Redis learning (I)
Spark 结构化流写入Hudi 实践