当前位置:网站首页>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])
}
}
边栏推荐
- Common software open source protocols
- WARNING: You are using pip version 21.3.1; however, version 22.0.3 is available. Prompt to upgrade pip
- 从0开始使用pnpm构建一个Monorepo方式管理的demo
- [solution to the new version of Flink without bat startup file]
- Construction and test of TFTP server under unbuntu (Debian)
- [kotlin learning] classes, objects and interfaces - classes with non default construction methods or attributes, data classes and class delegates, object keywords
- Hudi learning notes (III) analysis of core concepts
- 【Kotlin学习】类、对象和接口——带非默认构造方法或属性的类、数据类和类委托、object关键字
- LeetCode每日一题(1996. The Number of Weak Characters in the Game)
- LeetCode每日一题(1162. As Far from Land as Possible)
猜你喜欢

图像修复方法研究综述----论文笔记

CATIA automation object architecture - detailed explanation of application objects (I) document/settingcontrollers
![[CSDN] C1 training problem analysis_ Part III_ JS Foundation](/img/b2/68d53ad09688f7fc922ac65e104f15.png)
[CSDN] C1 training problem analysis_ Part III_ JS Foundation

Flask+supervisor installation realizes background process resident

NPM install installation dependency package error reporting solution

文件系统中的目录与切换操作

2022-2-14 learning xiangniuke project - Session Management

Hudi 数据管理和存储概述

Build a solo blog from scratch
![[kotlin learning] classes, objects and interfaces - define class inheritance structure](/img/66/34396e51c59504ebbc6b6eb9831209.png)
[kotlin learning] classes, objects and interfaces - define class inheritance structure
随机推荐
Detailed steps of windows installation redis
全球KYC服务商ADVANCE.AI 活体检测产品通过ISO国际安全认证 产品能力再上一新台阶
LeetCode每日一题(2115. Find All Possible Recipes from Given Supplies)
Idea uses the MVN command to package and report an error, which is not available
Build a solo blog from scratch
Redis learning (I)
Jestson nano custom root file system creation (supports the smallest root file system of NVIDIA Graphics Library)
LeetCode每日一题(516. Longest Palindromic Subsequence)
【Kotlin学习】高阶函数的控制流——lambda的返回语句和匿名函数
小王叔叔的博客目录【持续更新中】
Win10安装ELK
Liteide is easy to use
【Kotlin学习】类、对象和接口——带非默认构造方法或属性的类、数据类和类委托、object关键字
Overview of image restoration methods -- paper notes
Hudi 快速体验使用(含操作详细步骤及截图)
图像修复方法研究综述----论文笔记
Solve editor MD uploads pictures and cannot get the picture address
LeetCode每日一题(1856. Maximum Subarray Min-Product)
LeetCode每日一题(968. Binary Tree Cameras)
Flink学习笔记(九)状态编程