LintCode "Copy Books"
Classic DP. The initial intuitive O(k*n^2) solution is like this:
class Solution {
public:
/**
* @param pages: a vector of integers
* @param k: an integer
* @return: an integer
*/
int copyBooks(vector<int> &pages, int k) {
size_t n = pages.size();
if(k > n)
{
return *max_element(pages.begin(), pages.end());
} // Prefix Sums
vector<long long> psum(n);
for(int i = ; i < n; i ++)
psum[i] = i == ? pages[i] : (psum[i - ] + pages[i]); // DP
vector<vector<long long>> dp(n + , vector<long long>(k + , INT_MAX));
for(int i = ; i <= n; i ++)
dp[i][] = psum[i - ]; for(int i = ; i <= k; i ++) // person
for(int b = i; b <= n; b ++) // book
for(int c = i-; c < b; c ++) // prev book
{
long long last = dp[c][i - ];
long long cur = psum[b-] - psum[c - ];
dp[b][i] = min(dp[b][i], max(cur, last));
} return dp[n][k];
}
};
O(nk): http://sidbai.github.io/2015/07/25/Copy-Books/Point above:
long long last = dp[c][i - 1];
long long cur = psum[b-1] - psum[c - 1];
min(dp[b][i], max(cur, last));
dp[c][i-1] is mono-inc by c, cur is mono-dec. min(.., max(cur,last)) is V-like in 2D plane. So we can use 2-pointers to find the bottom of the V!
Or, binary search with O(nlg(sum/k)): https://github.com/kamyu104/LintCode/blob/master/C++/copy-books.cpp
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 pe ...
- LintCode - Copy List with Random Pointer
LintCode - Copy List with Random Pointer LintCode - Copy List with Random Pointer Web Link Descripti ...
- Copy Books
Description Given n books and the i-th book has pages[i] pages. There are k persons to copy these bo ...
- Copy Books II
Description Given n books and each book has the same number of pages. There are k persons to copy th ...
- [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( ...
- 二分难题 && deque
141. Sqrt(x) https://www.lintcode.com/problem/sqrtx/description?_from=ladder&&fromId=4 publi ...
- Leetcode Lect3 二分法总结
二分法模板 非递归版本: public class Solution { /** * @param A an integer array sorted in ascending order * @pa ...
- postgresql批量备份和恢复数据表
备份数据库:pg_dump -h localhost -U root demo02 > /home/arno/dumps/demo02.bak 恢复数据库:psql -h localhost - ...
随机推荐
- shell命令:删除当前.sh文件所在目录下的zip包,并且重新打包
filepath=$(cd ")"; pwd) packagePath="$filepath"/package zipPath="$filepath& ...
- ODBC接口规范
第1章 ODBC API基础 1.1 ODBC API句柄 ODBC API 实现数据库操作的手段是句柄.在ODBC中,使用不同的句柄(HANDLE)来标志环境(environment).连接 ...
- Think Python - Chapter 16 - Classes and functions
16.1 TimeAs another example of a user-defined type, we’ll define a class called Time that records th ...
- Linux常用命令整理 - imsoft.cnblogs
su 用户名 在不退出登陆的情况下,切换到另外一个人的身份如果用户名缺省,则切换到root状态会提示输入密码,密码不回显的. 在用su命令切换root用户时,使用“-”选项,这样可以将root的环境变 ...
- 转:如何理解c和c ++的复杂类型声明
本文作者girlrong是网易广州社区的C语言版版主,这篇文章被选在精华区.很是不错,不敢独享!据说她乐于助人,虚心诚恳,颇受网友欢迎.只可惜现在已退隐江湖了.在最近学习C语言过程中,了解些前辈大牛的 ...
- Codeforces Round #108 (Div. 2)
Codeforces Round #108 (Div. 2) C. Pocket Book 题意 给定\(N(N \le 100)\)个字符串,每个字符串长为\(M(M \le 100)\). 每次选 ...
- Scrum 项目2.0
阅读教材第8章,8.1~8.3节 P157~168,了解获取用户需求的办法,每个组可以选择一二加以应用. 8.4节P168-171 查阅NABCDA模型的具体说明. 2.SCRUM 流程的步骤 1 完 ...
- Caffe-windows上训练自己的数据
1.数据获取 在网上选择特定类别,下载相应的若干张图片.可以网页另存或者图片下载器.本例中保存了小狗.菊花.梅花三类各两百多张. 2.重命名 import os import os.path root ...
- phpstorm编辑器智能提示框架代码
按照上面的步骤就可以智能提示代码了!
- 转--基于MVC4+EasyUI的Web开发框架形成之旅--界面控件的使用
原文 http://www.cnblogs.com/wuhuacong/p/3317223.html 基于MVC4+EasyUI的Web开发框架形成之旅--界面控件的使用 在前面介绍了两篇关于我的基 ...