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 give book A[1], A[3] to one people, because book A[1] and A[3] is not continuous.) Each person have can copy one page per minute. Return the number of smallest minutes need to copy all the books.

您在真实的面试中是否遇到过这个题?

Yes

样例

Given array A = [3,2,4],
k = 2.

Return 5( First person
spends 5 minutes to copy book 1 and book 2 and second person spends 4 minutes to copy book 3. )

挑战

Could you do this in O(n*k) time
?

分析:只会O(n*lgM),M为所有数的总和,可以看到,求最小要多少分钟,也就是大于这个数,都是可以的,而小于这个数则都是不可以的,于是可以利用二分来找到这个点

代码:

class Solution {
public:
    /**
     * @param pages: a vector of integers
     * @param k: an integer
     * @return: an integer
     */
    int copyBooks(vector<int> &pages, int k) {
        // write your code here
        int l = 0;
        int r = 99999999;
        while(l<r)
        {
            int mid = (l+r)/2;
            if(check(mid,pages,k))
            {
                r = mid;
            }
            else
                l = mid+1;
        }
        return l;

    }
    bool check(int index,vector<int>& pages,int k)
    {
        int cnt = 0;
        int sum = 0;
        int i = 0;
        while(i<pages.size())
        {
            if(sum+pages[i]<=index)
            {
                sum+=pages[i++];
            }
            else if(pages[i]<=index)
            {
                cnt++;
                sum = 0;
            }
            else
                return false;
        }
        if(sum!=0)
            cnt++;
        return cnt<=k;
    }
};

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-10-16 16:27:16

LintCode-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

LintCode - Copy List with Random Pointer

LintCode - Copy List with Random Pointer LintCode - Copy List with Random Pointer Web Link Description Code - C Tips Web Link http://www.lintcode.com/en/problem/copy-list-with-random-pointer/ Description A linked list is given such that each node con

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

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.

动态规划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

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