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;
}
}

  

最新文章

  1. [转载]windows 7 IIS 7.5 ASP.Net 文件上传大小限制
  2. c#中多线程同步Lock(锁)的研究以及跨线程UI的操作
  3. POJ - 3652 Persistent Bits
  4. 系统在某些情况下会自动调节UIScrollView的contentInset
  5. C++-多重继承的注意点
  6. 学习总结 java 数据库 ResultSet 、PreparedStatement
  7. 理解angularJS中作用域$scope
  8. swjtu 1962 A+B(模拟)
  9. git for windows (又名 msysgit)如何记住用户名和密码
  10. spring data jpa使用懒操作
  11. JEESZ-Zookeeper集群安装
  12. cloudera-manager所有服务提示时钟偏差问题解决办法
  13. 2017(5)软件架构设计,web系统的架构设计,数据库系统,分布式数据库
  14. BZOJ2130 : 魔塔
  15. db2 load选项
  16. Device does not seem to be present [常见错误解决]
  17. node基础 npm、module、exports、require
  18. fpm制作rpm包
  19. JAVA 图像操作辅助类
  20. HTML通过jQuery传值赋值

热门文章

  1. [SVN] - 使用 TortoiseSVN 进行文件比对非常慢 之 解决
  2. Burp Suite的安装与使用
  3. IAR_EW_MSP430下载
  4. OpenCV学习笔记5
  5. Linux 进程地址空间及原理
  6. MySQL数据安全
  7. 微信小程序的页面跳转==编程式导航传参 和 标签的方法传参==以及如何过去传递过来的参数
  8. X86驱动:恢复SSDT内核钩子
  9. vue 等比例截图组件,支持缩放和旋转
  10. springboot2.x配置druid sql监控