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. 【转】How-To-Ask-Questions-The-Smart-Way

    提问的智慧 How To Ask Questions The Smart Way Copyright © 2001,2006,2014 Eric S. Raymond, Rick Moen 本指南英文 ...

  2. Windows上的文件合并命令

    从Linux转到Windowns后,发现很多好用的shell命令都没有了,但实际情况是Windows一样有DOS时代的命令窗口,在CLI年代用DOS的人也要干活. 比如,今天想将几个单独的sql文件整 ...

  3. Apache HttpClient使用之阻塞陷阱

    前言: 之前做个一个数据同步的定时程序. 其内部集成了某电商的SDK(简单的Apache Httpclient4.x封装)+Spring Quartz来实现. 原本以为简单轻松, 喝杯咖啡就高枕无忧的 ...

  4. Pickpic和FarStone走好..GreenShot上岗

    很早前就看過這丫的,以前就是拒絕改變,換過n多切圖工具了,除了題目中倆 還自己用AHK過一款,但最後還是停在Pickpic因為要上FTP比較快 今天在SourceForge亂逛邂逅了這貨,才知道原來” ...

  5. H5标签-canvas实现颜色拾取功能

    HTML5 <canvas> 标签是用于绘制图像,不过,<canvas> 元素本身并没有绘制能力(它仅仅是图形的容器),必须使用脚本(通常是 JS)来完成实际的绘图任务. &l ...

  6. 转:Struts标签checkbox使用总结(默认选择设置)

    在使用struts标签html:checkbox 的时候,如何让checkbox框默认是选中的,一般情况 下都是当formbean里面该property的值和标签上value给定的值相等的时候,生成的 ...

  7. C#部分---利用arraylist集合做滚动抽奖;

    输入多个手机号码,放到集合中,进行三秒钟的滚动抽奖:随机显示号码,清空,再显示: 1.收集号码: 2.每隔三秒进行抽奖,及作弊代码,哈哈哈: 3.System.Threading.Thread.Sle ...

  8. alias sample method——运行时间复杂度为O(1)的抽样算法

    根据离散离散概率分布抽样是一个常见的问题.这篇文章将介绍运行时间复杂度为O(1)的 alias method 抽样算法思想. 下面举例说明: 比如 a,b,c,d 的概率分别为 0.1,0.2,0.3 ...

  9. leetcode 146. LRU Cache ----- java

    esign and implement a data structure for Least Recently Used (LRU) cache. It should support the foll ...

  10. POJ 1043 What's In A Name?(唯一的最大匹配方法)

                                                            What's In A Name? Time Limit: 1000MS   Memor ...