Copy Books II
Description
Given n
books and each book has the same number of pages. There are k
persons to copy these books and the i-th
person needs times[i]
minutes to copy a book.
Each person can copy any number of books and they start copying at the same time. What's the best strategy to assign books so that the job can be finished at the earliest time?
Return the shortest time.
Example
Example 1:
Input: n = 4, times = [3, 2, 4]
Output: 4
Explanation:
First person spends 3 minutes to copy 1 book.
Second person spends 4 minutes to copy 2 books.
Third person spends 4 minutes to copy 1 book.
Example 2:
Input: n = 4, times = [3, 2, 4, 5]
Output: 4
Explanation: Use the same strategy as example 1 and the forth person does nothing.
思路:
可以使用二分或者动态规划解决这道题目. 不过更推荐二分答案的写法, 它更节省空间, 思路简洁, 容易编码.
对于假定的时间上限 tm
我们可以使用贪心的思想判断这 k
个人能否完成复印 n
本书的任务: 每个人都在规定时间内尽可能多地复印, 判断他们复印的总数是否不小于 n
即可.
而时间上限 tm
与可否完成任务(0或1)这两个量之间具有单调性关系, 所以可以对 tm
进行二分查找, 查找最小的 tm
, 使得任务可以完成.
public class Solution {
/**
* @param n: An integer
* @param times: an array of integers
* @return: an integer
*/
public int copyBooksII(int n, int[] times) {
if (n == 0) {
return 0;
}
int left = 0, right = times[0] * n;
while (left < right) {
int mid = left + (right - left) / 2;
if (check(n, times, mid)) {
right = mid;
} else {
left = mid + 1;
}
}
return left;
} private boolean check(int n, int[] times, int limit) {
int count = 0;
for (int i : times) {
count += limit / i;
}
return count >= n;
}
}
Copy Books II的更多相关文章
- [LintCode] Copy Books 复印书籍
Given an array A of integer with size of n( means n books and number of pages of each book) and k pe ...
- Copy Books
Description Given n books and the i-th book has pages[i] pages. There are k persons to copy these bo ...
- LintCode "Copy Books"
Classic DP. The initial intuitive O(k*n^2) solution is like this: class Solution { public: /** * @pa ...
- 二分难题 && deque
141. Sqrt(x) https://www.lintcode.com/problem/sqrtx/description?_from=ladder&&fromId=4 publi ...
- [LintCode]——目录
Yet Another Source Code for LintCode Current Status : 232AC / 289ALL in Language C++, Up to date (20 ...
- 九章lintcode作业题
1 - 从strStr谈面试技巧与代码风格 必做题: 13.字符串查找 要求:如题 思路:(自写AC)双重循环,内循环读完则成功 还可以用Rabin,KMP算法等 public int strStr( ...
- Java Algorithm Problems
Java Algorithm Problems 程序员的一天 从开始这个Github已经有将近两年时间, 很高兴这个repo可以帮到有需要的人. 我一直认为, 知识本身是无价的, 因此每逢闲暇, 我就 ...
- Leetcode Lect3 二分法总结
二分法模板 非递归版本: public class Solution { /** * @param A an integer array sorted in ascending order * @pa ...
- postgresql批量备份和恢复数据表
备份数据库:pg_dump -h localhost -U root demo02 > /home/arno/dumps/demo02.bak 恢复数据库:psql -h localhost - ...
随机推荐
- [转帖]一文尽懂 USB4
一文尽懂 USB4 https://www.ithome.com/0/451/062.htm 今年 3 月份,USB Promoter Group(领导小组)首次发布了 USB4 规范,即下一代 US ...
- [VS] - 手工打开 WCF 客户端调试工具
操作步骤 1. 在开始菜单中找到 Visual Studio 命令行工具 2. 输入命令 wcftestclient 即可打开 WCF 客户端测试工具 参考资料http://www.cnblogs.c ...
- (1)Spirng Boot 入门(笔记)
文章目录 简介 优点 Hello World 打包成可执行 jar 细节探究 主程序类,主入口类上面的注解 自动生成的项目结构分析 简介 Spring Boot 帮助我们简化 Spring 应用开发: ...
- JDBC预编译statement(preparedstatement)和statement的比较、execute与executeUpdate的区别
和 Statement一样,PreparedStatement也是用来执行sql语句的与创建Statement不同的是,需要根据sql语句创建PreparedStatement除此之外,还能够通过设置 ...
- Word 中批量修改所有表格格式样式
1. 引言 我们在使用Word排版编写书籍时候,可能会带有许多表格,上百,甚至上千个表格都是有可能的.这么多的表格对于后期的样式修改是非常不利的,有什么好的方法能够一次性修改文档中所有的表格,将其统一 ...
- python 之 面向对象基础(继承与派生,经典类与新式类)
7.2 继承与派生 7.21继承 1.什么是继承? 继承是一种新建类的的方式,在python中支持一个子类继承多个父类.新建的类称为子类或者派生类,父类又可以称为基类或者超类,子类会”遗传“父类的属性 ...
- ~request库的使用
官方文档: (中文)http://cn.python-requests.org/zh_CN/latest/ (英文)https://2.python-requests.org//en/master/a ...
- python第三天---列表的魔法
# list 列表 # 中括号括起来,逗号分隔每个元素, # 列表中可以是数字字符串.列表等都可以放进去 list1 = [123, "book", "手动", ...
- PAT甲级题分类汇编——树
本文为PAT甲级分类汇编系列文章. AVL树好难!(其实还好啦~) 我本来想着今天应该做不完树了,没想到电脑里有一份讲义,PPT和源代码都有,就一遍复习一遍抄码了一遍,更没想到的是编译一遍通过,再没想 ...
- PowerBuilder学习笔记之删除和加载PBL文件的方法
删除PBL目录的方法:直接点删除键删除 加载PBL文件的方法:点Browse按钮选择PBL文件