当前位置:网站首页>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])
}
}
边栏推荐
- Modify idea code
- Logstash+jdbc data synchronization +head display problems
- Basic knowledge of database design
- 图像修复方法研究综述----论文笔记
- Filter comments to filter out uncommented and default values
- [set theory] order relation (eight special elements in partial order relation | ① maximum element | ② minimum element | ③ maximum element | ④ minimum element | ⑤ upper bound | ⑥ lower bound | ⑦ minimu
- PowerDesigner does not display table fields, only displays table names and references, which can be modified synchronously
- 2022-1-6 Niuke net brush sword finger offer
- [untitled] use of cmake
- [point cloud processing paper crazy reading classic version 10] - pointcnn: revolution on x-transformed points
猜你喜欢

Solve POM in idea Comment top line problem in XML file

Vs2019 configuration opencv3 detailed graphic tutorial and implementation of test code

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

Spark 集群安装与部署

CSDN markdown editor help document
![[set theory] order relation (chain | anti chain | chain and anti chain example | chain and anti chain theorem | chain and anti chain inference | good order relation)](/img/fd/c0f885cdd17f1d13fdbc71b2aea641.jpg)
[set theory] order relation (chain | anti chain | chain and anti chain example | chain and anti chain theorem | chain and anti chain inference | good order relation)

【点云处理之论文狂读前沿版13】—— GAPNet: Graph Attention based Point Neural Network for Exploiting Local Feature

Hudi 数据管理和存储概述

LeetCode每日一题(1162. As Far from Land as Possible)

LeetCode 515. Find the maximum value in each tree row
随机推荐
[point cloud processing paper crazy reading frontier version 10] - mvtn: multi view transformation network for 3D shape recognition
Powerdesign reverse wizard such as SQL and generates name and comment
Just graduate student reading thesis
【毕业季|进击的技术er】又到一年毕业季,一毕业就转行,从动物科学到程序员,10年程序员有话说
LeetCode 515. Find the maximum value in each tree row
[point cloud processing paper crazy reading frontier edition 13] - gapnet: graph attention based point neural network for exploring local feature
Install database -linux-5.7
【点云处理之论文狂读经典版14】—— Dynamic Graph CNN for Learning on Point Clouds
Derivation of Fourier transform
Crawler career from scratch (I): crawl the photos of my little sister ① (the website has been disabled)
CATIA automation object architecture - detailed explanation of application objects (I) document/settingcontrollers
Construction of simple database learning environment
Recommend a low code open source project of yyds
[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
Detailed steps of windows installation redis
【点云处理之论文狂读前沿版9】—Advanced Feature Learning on Point Clouds using Multi-resolution Features and Learni
[untitled] use of cmake
C language programming specification
图像修复方法研究综述----论文笔记
Redis learning (I)