It can be solved using a Dynamic Programming approach where the state is defined as DP[i][j]
where i
refers to the ending index of a day and j
keeps the count of the days so far. You can move to the next state and take the maximum sum of a day corresponding to the current move and then can compare it to the overall minimum.
I have written a recursive Dynamic programming solution in c++, it might be a little tough to understand as to how the state transitions are working you might need to look into Dynamic programming with memoisation to understand it.
#include <iostream>
#define INF 1000000000
using namespace std;
int n, m, dist[100] = {1,5,2,6,8,3,2}, dp[1000][1000];
int solve(int index, int count){
if(index == n){
if(count == m) return 0;
else return INF;
}
if(dp[index][count] != -1) return dp[index][count];
int sum = 0, ans = INF;
for(int i = index;i < n;i++){
sum += dist[i];
int val = max(sum, solve(i+1, count+1));
ans = min(ans, val);
}
return dp[index][count] = ans;
}
int main() {
// your code goes here
n = 7, m = 3;
for(int i = 0;i < 1000;i++) for(int j = 0;j < 1000;j++) dp[i][j] = -1;
cout << solve(0, 0) << endl;
return 0;
}
Link to solution Ideone : https://ideone.com/glHsgF
O(n * m)
– Foskett[1,5,2,6,8,3,2], [], []
where the minimum day length is 0 which is better than 6. In any case, in a naive solution you could just use binpacking and use binary search on the volume parameter. – Kwonm
values to be minimized. – Foskett