Maximum Profit from Scheduled Deliveries — Problem Statement & Solution Guide
Problem Description
Given a list of delivery tasks with their respective profits and deadlines, determine the maximum profit that can be achieved by scheduling these tasks without exceeding their deadlines, considering that each task can only be scheduled once and tasks cannot be scheduled in parallel.
Examples
Input
[[1, 2, 3], [2, 4, 5], [3, 6, 7]]
Output
12
Explanation: Step-by-step: We can schedule task with profit 2 on day 2 and task with profit 10 on day 3. So, the maximum profit is 2 + 10 = 12.
Input
[[1, 2, 3], [2, 4, 5], [3, 6, 7]]
Output
6
Explanation: Step-by-step: We can schedule task with profit 1 on day 1, task with profit 2 on day 2, and task with profit 3 on day 3. So, the maximum profit is 1 + 2 + 3 = 6.
Constraints
- The number of tasks will not exceed 10^4.
- Each task's profit and deadline will be positive integers.
- The deadlines of tasks will not exceed 10^4.
- The maximum possible profit will not exceed 10^6.
Optimal Approach & Strategy
The optimized approach uses dynamic programming to efficiently track the maximum achievable profit at each deadline, avoiding redundant computations and ensuring an optimal scheduling strategy with a time complexity of O(n^2).
Brute Force Approach
The brute-force approach would involve generating all possible permutations of tasks and calculating the total profit for each permutation, selecting the one that yields the highest profit without exceeding deadlines.
Verified Code Solutions
class Solution {
public int maxProfit(int[][] tasks) {
Arrays.sort(tasks, (a, b) -> a[2] - b[2]);
int max_profit = 0;
int deadline = 0;
for (int[] task : tasks) {
if (task[2] > deadline) {
deadline = task[2];
max_profit += task[0];
}
}
return max_profit;
}
}def maxProfit(tasks):
tasks.sort(key=lambda x: x[2])
max_profit = 0
deadline = 0
for profit, weight, day in tasks:
if day > deadline:
deadline = day
max_profit += profit
return max_profitAsked in Top Tech Interviews
Solve in Interative Editor
Ready to test your code? Open our built-in compiler, run custom test suites, and see detailed complexity analysis reports instantly.