Copy Books
2015-12-01 21:03
330 查看
Given an array A of integer with size of n( means n books and number of pages of each book) and k people to copy the book. You must distribute the continuous id books to one people to copy. (You can give book A[1],A[2]
to one people, but you cannot give book A[1], A[3] to one people, because book A[1] and A[3] is not continuous.) Each person have can copy one page per minute. Return the number of smallest minutes need to copy all the books.
样例
Given array A =
k =
Return
spends 5 minutes to copy book 1 and book 2 and second person spends 4 minutes to copy book 3. )
挑战
Could you do this in
?
to one people, but you cannot give book A[1], A[3] to one people, because book A[1] and A[3] is not continuous.) Each person have can copy one page per minute. Return the number of smallest minutes need to copy all the books.
样例
Given array A =
[3,2,4],
k =
2.
Return
5( First person
spends 5 minutes to copy book 1 and book 2 and second person spends 4 minutes to copy book 3. )
挑战
Could you do this in
O(n*k)time
?
class Solution { public: /** * @param pages: a vector of integers * @param k: an integer * @return: an integer */ int copyBooks(vector<int> &pages, int k) { // write your code here int n = pages.size(); if (n < 1 || k < 1) { return 0; } if (k >= n) { int result = INT_MIN; for (int i = 0; i < n; i++) { if (pages[i] > result) { result = pages[i]; } } return result; } int buf[k] ; buf[0][0] = pages[0]; for (int i = 1; i < n; i++) { buf[0][i] = buf[0][i-1] + pages[i]; } for (int i = 1; i < k; i++) { for (int j = n-1; j >= i; j--) { int sum = 0; buf[i][j] = INT_MAX; for (int k = j; k >= i-1; k--) { int temp = max(buf[i-1][k], sum); buf[i][j] = min(buf[i][j], temp); sum += pages[k]; } } } return buf[k-1][n-1]; } };
相关文章推荐
- Linux作业控制
- 积跬步-至千里系列之一:Linux系统学习的一些初级命令
- shell 学习记录
- 在CentOS7上安装Python3.5.0,及其与旧版本Python2.7.5的共存问题笔记
- linux 驱动理论学习阶段
- 深入分析JavaWeb Item2 -- Tomcat服务器学习和使用
- linux 用户、用户组不能是全数字
- libGDX执行DesktopLauncher提示Couldn't load file: badlogic.jpg
- 使用Nginx作为负载均衡器
- Docker初级学习-安装Docker
- linux中/etc与/var目录,各是什么意思?这两个目录下的文件有什么特点?
- linux0.11 内核启动代码分析(一)
- Linux下搭建SVN服务器
- apache 配置
- hadoop和spark搭建记录
- Qt5.5.0编译移植到Linux-Arm-A9
- SERVLET中的doGet与doPost两个方法之间的区别
- Tomcat.localhost(Problem)
- Linux-CentOS 安装FFmpeg
- Tomcat+Solr构建企业级搜索引擎