Tasks and Deadlines

You have to process n tasks. Each task has a duration and a deadline, and you will process the tasks in some order one after another. Your reward for a task is where d is its deadline and f is your finishing time. (The starting time is 0, and you have to process all tasks even if a task would yield a negative reward.)

What is your maximum reward if you act optimally?

Example:

Input:  n = 3, arr = {{6, 10}, {8, 15}, {5, 12}}
Output: 2

Approach

C++

#include <bits/stdc++.h>
using namespace std;

long long tasksAndDeadline(long long n,
                           vector<vector<long long>> &arr)
{
    sort(arr.begin(), arr.end());
    long long ans = 0pre = 0;
    for (long long i = 0i < ni++)
    {
        pre += arr[i][0];
        ans += arr[i][1] - pre;
    }
    return ans;
}

int main()
{

    long long n = 3;

    vector<vector<long long>> arr = {{610},
                                     {815},
                                     {512}};

    cout << tasksAndDeadline(narr<< "\n";

    return 0;
}


No comments:

Post a Comment