您的位置:首页 > 运维架构

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 =
[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];
}
};
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: