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 continously, but he can not copy the 1st book, 2nd book and 4th book (without 3rd book).

They start copying books at the same time and they all cost 1 minute to copy 1 page of a book. What‘s the best strategy to assign books so that the slowest copier can finish at earliest time?

Return the shortest time that the slowest copier spends.

The sum of book pages is less than or equal to 2147483647

Example

Example 1:

Input: pages = [3, 2, 4], k = 2
Output: 5
Explanation:
    First person spends 5 minutes to copy book 1 and book 2.
    Second person spends 4 minutes to copy book 3.

Example 2:

Input: pages = [3, 2, 4], k = 3
Output: 4
Explanation: Each person copies one of the books.

Challenge

O(nk) time

思路:

可以使用二分或者动态规划解决这道题目. 不过更推荐二分答案的写法, 它更节省空间, 思路简洁, 容易编码.

对于假定的时间上限 tm 我们可以使用贪心的思想判断这 k 个人能否完成复印 n 本书的任务: 将尽可能多的书分给同一个人, 判断复印完这 n 本书需要的人数是否不大于 k 即可.

而时间上限 tm 与可否完成任务(0或1)这两个量之间具有单调性关系, 所以可以对 tm 进行二分查找, 查找最小的 tm, 使得任务可以完成.

public class Solution {
    /**
     * @param pages: an array of integers
     * @param k: An integer
     * @return: an integer
     */
    public int copyBooks(int[] pages, int k) {
        if (pages == null || pages.length == 0) {
            return 0;
        }

        int left = 0;
        int right = Integer.MAX_VALUE;

        while (left < right) {
            int mid = left + (right - left) / 2;
            if (check(pages, k, mid)) {
                right = mid;
            } else {
                left = mid + 1;
            }
        }
        if (check(pages, k, left)) {
            return left;
        }
        return right;
    }

    private boolean check(int[] pages, int k, int limit) {
        int num = 0;
        int left = 0;
        for (int item : pages) {
            if (item > limit) {
                return false;
            }
            if (item > left) {
                num++;
                left = limit;
            }
            left -= item;
        }
        return num <= k;
    }
}

  

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

时间: 2024-09-29 18:28:21

Copy Books的相关文章

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

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

CS3K.com 九章算法强化班

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

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类型:对于维度

动态规划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. 将字符串每一段划分成字符串最少划分几次      划分最少,也就是回文串最

714 - Copying Books——[贪心、二分查找]

Before the invention of book-printing, it was very hard to make a copy of a book. All the contents had to be re-written by hand by so called scribers. The scriber had been given a book and after several months he finished its copy. One of the most fa