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的更多相关文章

  1. [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 ...

  2. Copy Books

    Description Given n books and the i-th book has pages[i] pages. There are k persons to copy these bo ...

  3. LintCode "Copy Books"

    Classic DP. The initial intuitive O(k*n^2) solution is like this: class Solution { public: /** * @pa ...

  4. 二分难题 && deque

    141. Sqrt(x) https://www.lintcode.com/problem/sqrtx/description?_from=ladder&&fromId=4 publi ...

  5. [LintCode]——目录

    Yet Another Source Code for LintCode Current Status : 232AC / 289ALL in Language C++, Up to date (20 ...

  6. 九章lintcode作业题

    1 - 从strStr谈面试技巧与代码风格 必做题: 13.字符串查找 要求:如题 思路:(自写AC)双重循环,内循环读完则成功 还可以用Rabin,KMP算法等 public int strStr( ...

  7. Java Algorithm Problems

    Java Algorithm Problems 程序员的一天 从开始这个Github已经有将近两年时间, 很高兴这个repo可以帮到有需要的人. 我一直认为, 知识本身是无价的, 因此每逢闲暇, 我就 ...

  8. Leetcode Lect3 二分法总结

    二分法模板 非递归版本: public class Solution { /** * @param A an integer array sorted in ascending order * @pa ...

  9. postgresql批量备份和恢复数据表

    备份数据库:pg_dump -h localhost -U root demo02 > /home/arno/dumps/demo02.bak 恢复数据库:psql -h localhost - ...

随机推荐

  1. Java基础笔试练习(四)

    1.编译Java Application 源程序文件将产生相应的字节码文件,这些字节码文件的扩展名为( ). A.java B.class C.html D.exe 答案: B 解析: Java源程序 ...

  2. Java中XML的四种解析方式(二)

    三.JDOM解析 特征: 1.仅使用具体类,而不使用接口. 2.API大量使用了Collections类. import org.jdom2.Attribute; import org.jdom2.D ...

  3. C 语言字符串的比较

    C 语言字符串的比较 #include <stdio.h> #include <Windows.h> #include <string.h> int main(vo ...

  4. docker 实践五:端口映射和容器互联

    本篇是关于 docker 容器的端口映射和容器之间的互联内容. 注:环境为 CentOS7,docker 19.03. docker 的容器除了能连接网络外,在许多时候,我们需要让多个容器来协同完成任 ...

  5. PB在已经存在的datawindow中添加数据列的方法

    打开数据窗口,点击Data按钮 进入到数据源定义画板,选择要新增的列后,不要保存文件,直接点关闭,在提示框选是就可以了 新增的列值存不到数据库中,如果需要更新数据库中的值则:需要在数据窗口画板下,点击 ...

  6. spring整合MQ

    ---恢复内容开始--- 一. 导入依赖 <dependencies> <!-- ActiveMQ客户端完整jar包依赖 --> <dependency> < ...

  7. oracle sqlplus命令

    show和set命令是两条用于维护SQL*Plus系统变量的命令 SQL> show all --查看所有68个系统变量值 SQL> show user --显示当前连接用户 SQL> ...

  8. Emit用法

    [转自]https://blog.csdn.net/xiaouncle/article/details/52890037 本人是从0开始学习Emit的,在学习过程中比较困扰我的就是有很多指令不理解.不 ...

  9. robot framework 如何处理循环条件下面的变量自增

    下面举了一个基础栗子,可以运行的.${num}就是我需要的自增变量.有人也许会问为什么不用${i},不是我不想用,而是我${i}有其他用处,必须另外定义一个变量,需要注意的是定义变量的时候,应该在循环 ...

  10. J.U.C之AQS:同步状态的获取与释放

    此篇博客所有源码均来自JDK 1.8 在前面提到过,AQS是构建Java同步组件的基础,我们期待它能够成为实现大部分同步需求的基础.AQS的设计模式采用的模板方法模式,子类通过继承的方式,实现它的抽象 ...