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

  

原文地址:https://www.cnblogs.com/FLAGyuri/p/12077946.html

时间: 2024-10-07 23:20:05

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 people to copy the book. You must distribute the continuous id books to one people to copy. (You can give book A[1],A[2] to one people, but you cannot g

Copy Books

Description Given n books and the i-th book has pages[i] pages. There are k persons to copy these books. These books list in a row and each person can claim a continous range of books. For example, one copier can copy the books from i-th to j-th cont

CS3K.com 九章算法强化班

Advanced Data Structure -- Union Find Number of Islands 题目 思路I:BFS 避免相同位置的元素重复入列,访问过的元素都要标记为已访问.BFS为了得到下一个点的坐标,所以需要建立一个表示位置的坐标类. 思路II:并查集 等价于求集合中连通块的个数. Number of Islands II 题目 思路:并查集,把二维数组转化为一维father数组. LeetCode 547. Friend Circles 题目 思路:并查集的典型应用.题目

动态规划4 划分型动态规划

题目1:LintCode 108 Palindrome Partitioning II 题目2:LintCode 108 Palindrome Partitioning II Input: "aab" Output: 1 Explanation: Split "aab" once, into "aa" and "b", both palindrome. 将字符串每一段划分成字符串最少划分几次      划分最少,也就是回文串最

postgresql备份恢复数据库和批量导入导出数据到文件方法

备份数据库:pg_dump -h localhost -U root demo02 > /home/arno/dumps/demo02.bak 恢复数据库:psql -h localhost -U root -d demo <  demo.bak 备份表:pg_dump -h localhost -U root demo02 -t books > /home/arno/dumps/books.bak 恢复表:psql -h localhost -U root -d demo -t boo

用表达式树实现深拷贝功能

因为对表达式树有点兴趣,出于练手的目的,试着写了一个深拷贝的工具库.支持.net standard2.0或.net framework4.5及以上. GitHub地址https://github.com/blurhkh/DeepCopier nuget地址https://www.nuget.org/packages/DeepCopier 使用方法如下: 首先创建几个测试用的类型 public class Author { public string Name { get; set; } } pu

ETL面试题集锦

1. What is a logical data mapping and what does it mean to the ETL team? 什么是逻辑数据映射?它对ETL项目组的作用是什么? 答:逻辑数据映射(Logical Data Map)用来描述源系统的数据定义.目标数据仓库的模型以及将源系统的数据转换到数据仓库中需要做操作和处理方式的说明文档,通常以表格或Excel的格式保存如下的信息: 目标表名: 目标列名: 目标表类型:注明是事实表.维度表或支架维度表. SCD类型:对于维度表

ETL 的一些概念

1. What is a logical data mapping and what does it mean to the ETL team? 什么是逻辑数据映射?它对ETL项目组的作用是什么? 答: 逻辑数据映射(Logical Data Map)用来描述源系统的数据定义.目标数据仓库的模型以及将源系统的数据转换到数据仓库中需要做操作和处理方式的说明文档,通常以表格或Excel的格式保存如下的信息: 目标表名: 目标列名: 目标表类型:注明是事实表.维度表或支架维度表. SCD类型:对于维度

leetcode -day8 Copy List with Random Pointer &amp; Single Number I II

五一中间断了几天,开始继续... 1.  Copy List with Random Pointer A linked list is given such that each node contains an additional random pointer which could point to any node in the list or null. Return a deep copy of the list. 分析:剑指offer上的一道题目,分三步进行,首先复制每个链表结点