当前位置:网站首页>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])
}
}
边栏推荐
- ERROR: certificate common name “www.mysql.com” doesn’t match requested host name “137.254.60.11”.
- 一款开源的Markdown转富文本编辑器的实现原理剖析
- Serializer rewrite: update and create methods
- 2022-2-14 learning xiangniuke project - Session Management
- 2022-2-14 learning the imitation Niuke project - send email
- Redis learning (I)
- Overview of image restoration methods -- paper notes
- Banner - Summary of closed group meeting
- Jetson nano custom boot icon kernel logo CBOOT logo
- 用Redis实现分布式锁
猜你喜欢

Flink学习笔记(八)多流转换

Idea uses the MVN command to package and report an error, which is not available

Principles of computer composition - cache, connection mapping, learning experience

Win10 quick screenshot
![[kotlin learning] classes, objects and interfaces - classes with non default construction methods or attributes, data classes and class delegates, object keywords](/img/ee/d982fd9e1f2283e09ad1a81d0b61b5.png)
[kotlin learning] classes, objects and interfaces - classes with non default construction methods or attributes, data classes and class delegates, object keywords

PolyWorks script development learning notes (I) - script development environment

Hudi 快速体验使用(含操作详细步骤及截图)
![[CSDN] C1 training problem analysis_ Part III_ JS Foundation](/img/b2/68d53ad09688f7fc922ac65e104f15.png)
[CSDN] C1 training problem analysis_ Part III_ JS Foundation

2022-2-14 learning the imitation Niuke project - send email

Spark structured stream writing Hudi practice
随机推荐
Flask+supervisor installation realizes background process resident
【Kotlin疑惑】在Kotlin类中重载一个算术运算符,并把该运算符声明为扩展函数会发生什么?
Vscode Arduino installation Library
Powerdesign reverse wizard such as SQL and generates name and comment
Modify idea code
软件测试工程师是做什么的 通过技术测试软件程序中是否有漏洞
CATIA automation object architecture - detailed explanation of application objects (III) systemservice
Jetson nano custom boot icon kernel logo CBOOT logo
QT qstring:: number apply base conversion
[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
Equality judgment of long type
Filter comments to filter out uncommented and default values
Learning C language from scratch -- installation and configuration of 01 MinGW
Usage of pandas to obtain MySQL data
Esp32 at command does not respond
Analysis of the implementation principle of an open source markdown to rich text editor
Database execution error: SQL_ mode only_ full_ group_ by:
Construction and test of TFTP server under unbuntu (Debian)
Jenkins learning (III) -- setting scheduled tasks
Jenkins learning (II) -- setting up Chinese