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 - ...
随机推荐
- Open vSwitch FAQ (三)
Quality of Service (QoS) Q: How do I configure Quality of Service (QoS)? A: Suppose that you want to ...
- setSelection()和requestFocusFromTouch()
昨天我遇到一个问题,点击返回的时候要在onResume()中用setSelection()定位到刚才点击的item,因为点击item进入后,我又一直点击“下一个”按钮,但是返回的时候listview不 ...
- String的常规使用集合
今天先附上代码君: package com.jacob.javase; import java.io.UnsupportedEncodingException; /* *探讨String: * * ...
- js 小数取整的函数
1.丢弃小数部分,保留整数部分 js:parseInt(7/2) 2.向上取整,有小数就整数部分加1 js: Math.ceil(7/2) 3,四舍五入. js: Math.round(7/2) 4, ...
- Date 对象总结
Date对象,是操作日期和时间的对象.Date对象对日期和时间的操作只能通过方法. 无参数: var date=new Date(); console.log(date) // Mon Jun ...
- mysql 关联删除
参考网址:http://www.111cn.net/database/mysql/51146.htm 原网页广告太多,自己抄了下. 1.delete from t1 where 条件2.delete ...
- appium简明教程(6)——启动appium及android模拟器
一般情况下,我们都从命令行启动appium. windows下,dos命令窗口输入 appium 如果该命令报错,那么请重装appium npm install -g appium 如果安装出错,请自 ...
- jQuery - 设置内容和属性
设置内容 - text().html() 以及 val() 我们将使用前一章中的三个相同的方法来设置内容: text() - 设置或返回所选元素的文本内容 html() - 设置或返回所选元素的内容( ...
- (转)A Beginner's Guide To Understanding Convolutional Neural Networks Part 2
Adit Deshpande CS Undergrad at UCLA ('19) Blog About A Beginner's Guide To Understanding Convolution ...
- 拉动滚动条追加内容,无限延伸document高度 $(window).scroll(function(){if($(window).scrollTop() + $(window).height() == $(document).height()) { $("body").append(html) } })
$(document).ready(function() { // endless scrolling $(window).scroll(function() { if($(window).scrol ...