`

Algorithm之二分枚举之copy books

阅读更多
copy 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 famous scribers lived in the 15th century and his name was Xaverius Endricus Remius Ontius Xendrianus (Xerox). Anyway, the work was very annoying and boring. And the only way to speed it up was to hire more scribers.

由书法家誊写作家的书,然后出版。


1、题目


    
    /**
     https://www.lintcode.com/en/problem/copy-books/
     
     Given n books and the i-th book has A[i] pages. 
     You are given k people to copy the n books.

     n books list in a row and each person can claim a continuous range of the n books. 
     For example one copier can copy the books from i-th to j-th continuously, 
     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?
     
     
     :::Example:::
     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. )
    
     :::Challenge:::
     Could you do this in O(n*k) time ?


     
     *
     */
    

    




解法:



/**
  http://blog.csdn.net/shuangde800/article/details/7828695
 
    分析与总结:
    所化的总时间取决于所有抄写员中任务最多的那个,是经典的最大值最小化问题。
    LRJ《算法入门经典》P151页有介绍:

    二分最小值x,把优化问题转化为判定问题。
    设为P(x)。
    设所有数之和为M,则二分次数为O(logM)。
    计算P(x)的时间的时间复杂度为:O(n),//逐一验证
    因此总时间复杂度为:O(nlogM)
 */
/**===================================================================================*/
/**
 * www.jiuzhang.com/solutions/copy-books/
 * 
 * 本代码由九章算法编辑提供。版权所有,转发请注明出处。
 * - 九章算法致力于帮助更多中国人找到好的工作,教师团队均来自硅谷和国内的一线大公司在职工程师。
 * - 现有的面试培训课程包括:九章算法班,系统设计班,算法强化班,Java入门与基础算法班,
 *   Android 项目实战班,Big Data 项目实战班,
 * - 更多详情请见官方网站:http://www.jiuzhang.com/?source=code
 */

// version 1: Binary Search
// this version cost O(n log m) where n is the number of books and m is the sum of the pages.
public class CopyBooks {
    /**
     * @param pages: an array of books with pages number.
     * @param k: number of copiers.
     * @return: minimum max number.
     */
    public int copyBooks(int[] pages, int k) {
        if (pages.length == 0) {
            return 0;  
        }
        
        //1. calculate the sum of pages, and its max element.
        int sum = 0;
        int biggest = pages[0];
        for (int i = 0; i < pages.length; i++) {
            sum += pages[i];
            biggest = Math.max(biggest, pages[i]);
        }
        
        //2.use binary enumeration to calculate whether the mid 
        //  value is the proper one.
        int midStart = biggest;
        int end = sum;        
        while (midStart + 1 < end) {
            int mid = midStart + ((end - midStart) >> 1); // binary enumeration.
            // validate if the mid value can meet with k copiers.
            if (calculateCopiers(pages, mid) > k) { // when meet. midStart value is smaller. 
                midStart = mid;            // increase it. 
            } else {
                end = mid;                 // when can't meet. midStart value is larger.
            }
        }
        
        if (calculateCopiers(pages, midStart) <= k) {
            return midStart;
        }
        
        return end;
    }
    
    
    
    // with given limit number, calculate the copiers that needed.
    private int calculateCopiers(int[] pages, int limit) {
        if (pages.length == 0) {
            return 0;
        }
        
        int copiers = 1;
        int subTaskSum = pages[0]; // limit is always >= pages[0]
        for (int i = 1; i < pages.length; i++) {
            if (subTaskSum + pages[i] > limit) {
                copiers++;
                subTaskSum = 0;
            }
            subTaskSum += pages[i];
        }
        
        return copiers;
    }
    
    /**
    public static void main(String[] args) {
        int[] pages = new int[]{};
    }
    */
}













-

分享到:
评论

相关推荐

    Algorithm-awesome-algorithm-books.zip

    在"Algorithm-awesome-algorithm-books.zip"这个压缩包中,收藏了一系列优秀的算法书籍资源,旨在帮助学习者深入理解和掌握算法知识。 首先,算法的重要性不容忽视。在IT行业中,算法是开发高效软件和解决复杂问题...

    Algorithm-algorithm.zip

    - 搜索算法:如线性搜索、二分搜索、哈希搜索,帮助找到所需信息。 - 图算法:如Dijkstra算法、Floyd算法,用于处理网络结构的问题。 - 动态规划:如背包问题、最长公共子序列等,解决具有重叠子问题和最优子结构...

    C语言头文件 algorithm

    C语言头文件 algorithmC语言头文件 algorithmC语言头文件 algorithmC语言头文件 algorithmC语言头文件 algorithmC语言头文件 algorithmC语言头文件 algorithmC语言头文件 algorithmC语言头文件 algorithmC语言头文件...

    Algorithm-ToolGood.Algorithm.zip

    2. 搜索算法:如二分查找、广度优先搜索(BFS)和深度优先搜索(DFS),用于在数据结构中寻找特定元素或遍历结构。 3. 图论算法:包括最短路径算法(如Dijkstra算法、Floyd-Warshall算法)、最小生成树算法(如Prim算法...

    Algorithm-algorithm-princeton.zip

    算法的种类繁多,包括排序算法(如快速排序、归并排序)、查找算法(如二分查找、哈希查找)、图算法(如最短路径算法Dijkstra、最小生成树Prim)等。在普林斯顿的课程中,你会学习如何分析算法的时间复杂度和空间...

    Algorithm-algorithm-playground.zip

    二分查找在有序列表中效率极高,而哈希表查找则利用了键值对,提供快速的查找服务。 3. **图论算法**:如最短路径问题的Dijkstra算法、Floyd-Warshall算法,最小生成树的Prim算法和Kruskal算法,这些在网络优化、...

    Algorithm-algorithm-visualizer.zip

    2. **算法库**:包含了各种经典的排序、搜索、图论等算法实现,如冒泡排序、快速排序、二分查找、Dijkstra算法等。 3. **可视化引擎**:这部分负责将算法的运行过程转化为动态图形,通过颜色变化、箭头指示等视觉...

    Algorithm-PHP-algorithm.zip

    PHP中的二分查找算法是一种在有序数组中查找特定元素的高效策略,其时间复杂度为O(log n)。 此外,PHP中的图算法用于处理网络结构,如遍历、最短路径和最小生成树问题。Dijkstra算法和Floyd-Warshall算法常用于找到...

    Algorithm.rar_Algorithm Gossip_gossip_gossip algorithm_gossip算法

    经典算法 1.河内之塔 2.Algorithm Gossip: 费式数列 3. 巴斯卡三角形 ...6.Algorithm Gossip: 老鼠走迷官(二) 7.Algorithm Gossip: 骑士走棋盘 8.Algorithm Gossip: 八皇 9.Algorithm Gossip: 八枚银币 等

    latex 算法包algorithm2e

    ### Latex 算法包algorithm2e详解 #### 一、引言 在 LaTeX 中撰写算法时,通常会使用 `algorithm2e` 包来提高效率与美观性。此包由 Christophe Fiorio 开发并维护,适用于 LaTeX2e 版本。`algorithm2e` 是一个用于...

    Fireworks-Algorithm.rar_ fireworks algorithm_Fireworks Algorithm

    烟花算法程序大全Fireworks Algorithm

    Algorithm-way-to-algorithm.zip

    在"way-to-algorithm-master"目录中,我们可以找到不同类型的算法介绍,如排序算法(冒泡排序、选择排序、插入排序、快速排序、归并排序等)、查找算法(线性查找、二分查找等)、图算法(深度优先搜索、广度优先...

    Algorithm-algorithms.zip

    3. **算法分类**:在"Algorithm-algorithms.zip"中,可能包括排序算法(如冒泡排序、快速排序、归并排序)、查找算法(二分查找、哈希查找)、图算法(深度优先搜索、广度优先搜索)、树算法(二叉搜索树、AVL树、...

    Algorithm-algos.zip

    在"algos-master"这个子文件夹中,我们可以期待找到各种不同类型的算法实现,比如排序算法(快速排序、归并排序、堆排序)、搜索算法(二分查找、广度优先搜索、深度优先搜索)、图算法(Dijkstra算法、Floyd-...

    pentaho-aggdesigner-algorithm-5.1.5-jhyde-API文档-中文版.zip

    赠送jar包:pentaho-aggdesigner-algorithm-5.1.5-jhyde.jar; 赠送原API文档:pentaho-aggdesigner-algorithm-5.1.5-jhyde-javadoc.jar; 赠送源代码:pentaho-aggdesigner-algorithm-5.1.5-jhyde-sources.jar; ...

    Algorithm-JS-Algorithm.zip

    Algorithm-JS-Algorithm.zip,基于javascript的数据结构、算法,算法是为计算机程序高效、彻底地完成任务而创建的一组详细的准则。

    Algorithm-algorithm-library.zip

    2. **搜索算法**:二分查找、深度优先搜索(DFS)、广度优先搜索(BFS)、A*搜索算法等,用于在数据结构中查找目标元素或遍历所有可能性。 3. **图论算法**:Dijkstra算法、Floyd-Warshall算法、Prim算法和Kruskal...

    algorithm_头文件_说明

    `algorithm`头文件是C++标准库的一部分,它包含了大量用于处理序列(如数组、向量、列表等)的算法。这些算法不直接修改原始序列,而是通过迭代器操作,因此它们是函数式编程风格的体现。在STL(Standard Template ...

Global site tag (gtag.js) - Google Analytics