2020 Jan Gold Problem 1 Time is Mooney

From Wiki
Jump to navigation Jump to search

Official Problem Statement[edit]

Time is Mooney

Problem Statement[edit]

The goal of this problem is to determine the minimum amount of time needed to complete a sequence of tasks.

You are given a list of N tasks, each of which takes a certain amount of time to complete. You can only work on one task at a time, and you must complete all tasks in the given order. Additionally, you have a limited amount of time, T, to complete all the tasks.

Your task is to determine the minimum amount of time needed to complete all the tasks.

Solution[edit]

The solution to this problem can be solved using dynamic programming.

We create an array dp[N+1] where dp[i] represents the minimum amount of time needed to complete the first i tasks.

We can then fill the array using the following recurrence relation:

dp[i] = min(dp[i], dp[i-1] + time[i])

Where time[i] is the time required to complete the i-th task.

We can then iterate through the array and find the minimum amount of time needed to complete all tasks.

The following is a C++ implementation of the solution:

int N; // number of tasks
int T; // total time available
int time[N+1]; // time required to complete each task
int dp[N+1]; // dp[i] represents the minimum amount of time needed to complete the first i tasks

dp[0] = 0;

for (int i = 1; i <= N; i++) {
    dp[i] = T + 1; // set dp[i] to a value greater than T
    for (int j = 0; j < i; j++) {
        dp[i] = min(dp[i], dp[j] + time[i]);
    }
}

int ans = T + 1;
for (int i = 0; i <= N; i++) {
    ans = min(ans, dp[i]);
}

if (ans > T) {
    cout << "Not possible" << endl;
} else {
    cout << ans << endl;
}