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"的更多相关文章

  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. LintCode - Copy List with Random Pointer

    LintCode - Copy List with Random Pointer LintCode - Copy List with Random Pointer Web Link Descripti ...

  3. Copy Books

    Description Given n books and the i-th book has pages[i] pages. There are k persons to copy these bo ...

  4. Copy Books II

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

  5. [LintCode]——目录

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

  6. 九章lintcode作业题

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

  7. 二分难题 && deque

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

  8. Leetcode Lect3 二分法总结

    二分法模板 非递归版本: public class Solution { /** * @param A an integer array sorted in ascending order * @pa ...

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

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

随机推荐

  1. MySQL索引建立与删除

    #添加索引alter table 表名 add index 索引名称(列名1, 列名2);alter table 表名 add index 索引名称(列名1, 列名2, 列名3);alter tabl ...

  2. node模拟http服务器session机制-我们到底能走多远系列(36)

    我们到底能走多远系列(36) 扯淡: 年关将至,总是会在一些时间节点上才感觉时光飞逝,在平时浑浑噩噩的岁月里都浪费掉了太多的宝贵.请珍惜! 主题:      我们在编写http请求处理和响应的代码的时 ...

  3. 课堂所讲整理:HTML--8Window.document对象

    1.Window.document对象 一.找到元素: docunment.getElementById("id"):根据id找,最多找一个:    var a =docunmen ...

  4. jQuery对下拉框Select操作总结

    jQuery对下拉框Select操作总结 转自网络,留做备用 jQuery获取Select元素,并选择的Text和Value: 1. $("#select_id").change( ...

  5. (转) Written Memories: Understanding, Deriving and Extending the LSTM

    R2RT   Written Memories: Understanding, Deriving and Extending the LSTM Tue 26 July 2016 When I was ...

  6. java打包压缩文件

    package com.it.simple.util; import java.io.BufferedOutputStream;import java.io.ByteArrayOutputStream ...

  7. weblogic管理2 - 创建并启动一个managed server

    创建一个managed server. 1.  进入网页console管理页面,如:http://10.100.25.14:7001/console     , 先点击->服务器 (红色标记框) ...

  8. 最大化 AIX 上的 Java 性能,第 1 部分: 基础

    http://www.ibm.com/developerworks/cn/aix/library/es-Javaperf/es-Javaperf1.html 最大化 AIX 上的 Java 性能,第 ...

  9. 013. asp.net统计网站访问人数

    Global.asax中的代码: <%@ Application Language="C#" %> <script runat="server" ...

  10. fork炸弹

    众所周知,bash是一款极其强大的shell,提供了强大的交互与编程功能.这样的一款shell中自然不会缺少“函数”这个元素来帮助程序进行 模块化的高效开发与管理.于是产生了由于其特殊的特性,bash ...