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;
}
}
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 pe ...
- Copy Books II
Description Given n books and each book has the same number of pages. There are k persons to copy th ...
- LintCode "Copy Books"
Classic DP. The initial intuitive O(k*n^2) solution is like this: class Solution { public: /** * @pa ...
- [LintCode]——目录
Yet Another Source Code for LintCode Current Status : 232AC / 289ALL in Language C++, Up to date (20 ...
- 九章lintcode作业题
1 - 从strStr谈面试技巧与代码风格 必做题: 13.字符串查找 要求:如题 思路:(自写AC)双重循环,内循环读完则成功 还可以用Rabin,KMP算法等 public int strStr( ...
- postgresql批量备份和恢复数据表
备份数据库:pg_dump -h localhost -U root demo02 > /home/arno/dumps/demo02.bak 恢复数据库:psql -h localhost - ...
- ETL面试题集锦
1. What is a logical data mapping and what does it mean to the ETL team? 什么是逻辑数据映射?它对ETL项目组的作用是什么? 答 ...
- ETL面试题
1. What is a logical data mapping and what does it mean to the ETL team? 什么是逻辑数据映射?它对ETL项目组的作用是什么? 答 ...
- 二分难题 && deque
141. Sqrt(x) https://www.lintcode.com/problem/sqrtx/description?_from=ladder&&fromId=4 publi ...
随机推荐
- [EF] - "已有打开的与此 Command 相关联的 DataReader,必须首先将它关闭" 之解决
错误 解决 在 ConnectionString 中添加 MultipleActiveResultSets=true(适用于SQL 2005以后的版本).MultipleActiveResultSet ...
- scau 9502 ARDF一个变量的问题
哨兵变量flag不小心没 设置成0..所以一直WA 9502 ARDF 时间限制:1000MS 内存限制:65535K 提交次数:0 通过次数:0 题型: 编程题 语言: G++;GCC Des ...
- MySQL基础操作(一)
MySQL操作 一.创建数据库 # utf CREATE DATABASE 数据库名称 DEFAULT CHARSET utf8 COLLATE utf8_general_ci; # gbk CREA ...
- HTML5从入门到精通(千锋教育)免费电子版+PDF下载
本书是HTML5初学者极好的入门教材之一,内容通俗易懂.由浅入深.循序渐进.本书内容覆盖全面.讲解详细,其中包括标签语义化.标签使用规范.选择器类型.盒模型.标签分类.样式重置.CSS优化.Photo ...
- Spring AOP日志实现(三)--获取访问者用户名
通过Security获取访问者用户名: 也可以通过session来获取: 整体思路:
- Arm-Linux 移植 mtd-utils 1.x
有关文章:<mtd-utils 的 使用> 背景: 关于在公司的生产环境有关.不希望每次都在uboot下面做nand flash 的烧写:也觉得使用U盘升级的方法比较慢,而且有关的驱动不是 ...
- PAT-1021 Deepest Root (25 分) 并查集判断成环和联通+求树的深度
A graph which is connected and acyclic can be considered a tree. The height of the tree depends on t ...
- hdu1171 灵活的运用背包问题咯。。。 还有!!!! 合理的计算数组的范围!! wa了好多次!
Problem Description Nowadays, we all know that Computer College is the biggest department in HDU. Bu ...
- C# ObservableCollection两个字段排序的情况
相对于System.Linq的OrderBy及OrderByDescending方法,调用后产生IOrderedEnumberable对象,这个对象为排序后的返回值,但原对象未发生变化. 试想,有这种 ...
- centos7 GNOME 安装微信客户端
写在前边 最近新装了一个 centos7 GNOME 系统,用了很久了 win,突然转换 linux 桌面版,觉得焕然一新,给搬砖生活增添了一份新意 ~ 先看一下效果图: 怎么弄呢? 下载最新版本 t ...