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

  

Copy Books的更多相关文章

  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 II

    Description Given n books and each book has the same number of pages. There are k persons to copy th ...

  3. LintCode "Copy Books"

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

  4. [LintCode]——目录

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

  5. 九章lintcode作业题

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

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

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

  7. ETL面试题集锦

    1. What is a logical data mapping and what does it mean to the ETL team? 什么是逻辑数据映射?它对ETL项目组的作用是什么? 答 ...

  8. ETL面试题

    1. What is a logical data mapping and what does it mean to the ETL team? 什么是逻辑数据映射?它对ETL项目组的作用是什么? 答 ...

  9. 二分难题 && deque

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

随机推荐

  1. PC电脑看电视 / 电视直播 / 高清频道 / 直播源

    打开方式 PotPlayer + .m3u播放列表 效果图 .m3u播放列表 #EXTM3U #EXTINF:150,CCTV1综合 http://183.251.61.207/PLTV/888888 ...

  2. composer 无法配置命令行写入配置文件问题

    composer config repo.packagist composer https://packagist.phpcomposer.com 这条命令无法修改composer.json 添加中国 ...

  3. (一)Spring Security Demo 登陆与退出

    文章目录 配置springSecurityFilterChain过滤器 配置身份验证 加载配置 登陆项目 退出 下面的代码需要spring环境的支持: 看这个系列博客之前,需要这个博客,大概了解下 s ...

  4. java8 : 流

    package day02.com.offcn.test; import java.io.IOException; import java.nio.charset.Charset; import ja ...

  5. 链表习题(1)-设计一个递归算法,删除不带头结点的单链表L中所有值为x的结点

    /*设计一个递归算法,删除不带头结点的单链表L中所有值为x的结点*/ /* 算法思想:设f(L,x)的功能是删除以L为首结点指针的单链表中所有值等于x的结点, 则显然有f(L->next,x)的 ...

  6. python 的django项目复制方法

    python 的django项目复制方法 django_pyecharts_1修改为django_pyecharts_1_cs1.拷贝项目(确保原有项目是关闭状态下)2.粘贴项目并删除idea文件夹和 ...

  7. Map和TreeMap的特点

    Map的特点: 将键映射到值的对象 一个映射不能包含重复的键 每个键最多只能映射到一个值 TreeMap的特点: 无序,不允许重复(无序指元素顺序与添加顺序不一致) TreeMap集合默认会对键进行排 ...

  8. css 层叠 比较特殊性

    css 层叠: 多个相同的css声明(属性),应用到同一个元素上.当一个标签声明冲突时,浏览器会自动出发层叠机制 1:比较优先级 2:比较特殊性 3:比较源次序 依次经过上面的1,2,3的比较后,最终 ...

  9. Java 之 递归

    一.概述 递归:指在当前方法内调用自己的现象. 递归的分类: 递归分为两种,直接递归和简介递归 直接递归称为方法自身调用自己 间接递归可以 A 方法调用 B 方法,B 方法调用 C 方法,C 方法调用 ...

  10. (坑爹错误)记录prometheus中配置alertmanager.yml一次报错

    global: resolve_timeout: 5m #处理超时时间,默认为5min smtp_smarthost: 'smtp.sina.com:25' # 邮箱smtp服务器代理 smtp_fr ...