Select Best Single Task Type
Greedy & IntervalsAuto-graded · 20 test casesLast updated
Determine the task type index that maximizes total revenue within a given time budget using floor division and custom tie-breaking rules.
You are given a total time budget $T$ and $n$ available task types. Each task type $i$ (where $0 \le i < n$) is defined by:
duration[i]: the time taken to process one instance of task type $i$.revenue[i]: the profit earned for completing one instance of task type $i$.
You must choose exactly one task type to execute repeatedly until the total time limit $T$ is reached. Only fully completed tasks earn revenue; partial progress earns $0$.
The total revenue achieved by choosing task type $i$ is calculated as: $$\text{total_revenue}(i) = \lfloor T / \text{duration}[i] \rfloor \times \text{revenue}[i]$$
To select the winning task type index, options are compared using the following tuple of criteria, listed in order of priority (higher total criteria rank wins):
- Total Revenue: Higher total revenue is preferred.
- Index: Smaller index $i$ is preferred.
- Leftover Time: Smaller remaining time ($T \bmod \text{duration}[i]$) is preferred.
- Duration: Smaller
duration[i]is preferred.
Return the 0-based index $i$ of the best task type according to these selection rules.
Constraints
- $1 \le T \le 10^{12}$
- $1 \le n \le 10^5$
len(duration) == len(revenue) == n- $1 \le \text{duration}[i], \text{revenue}[i] \le 10^9$ for all $0 \le i < n$
Examples
Example 1
Input: maximize_revenue(10, [3, 4], [2, 3])
Output: 0
Example 2
Input: maximize_revenue(20, [5, 2], [10, 4])
Output: 0
Hints
Hint 1
For each index i, calculate the total revenue earned as `(T // duration[i]) * revenue[i]`.
Hint 2
Compare candidate task types using a tuple key: `(total_revenue, -i, -remaining_time, -duration)`.
Hint 3
Notice that since every element has a unique index i, index priority `-i` breaks all revenue ties immediately.
Worked solution
Easy Try it yourself first — then compare your approach, complexity and edge cases.
Approach
For each task type $i$, we can run at most $\lfloor T / \text{duration}[i] \rfloor$ complete tasks. The total revenue for task type $i$ is:
$$\text{total_revenue}(i) = \left\lfloor \frac{T}{\text{duration}[i]} \right\rfloor \times \text{revenue}[i]$$
We evaluate each index $i$ and rank candidates according to the preference rules:
- Maximize total revenue:
total - Minimize index:
-i - Minimize leftover time:
-(T % d) - Minimize duration:
-d
We construct a tuple key (total, -i, -(T % d), -d) for each candidate. Python compares tuples lexicographically element-by-element, matching the exact priority sequence requested.
Note that since all candidates in the input array have distinct indices $i$, the second element of the tuple (-i) is guaranteed to be unique for any two distinct items. Therefore, whenever two options tie on total revenue, the smaller index will always win.
Complexity
- Time Complexity: $\mathcal{O}(n)$, scanning through the task types once with $\mathcal{O}(1)$ arithmetic operations per element.
- Space Complexity: $\mathcal{O}(1)$ extra space beyond keeping track of the current best candidate key and index.
Python Reference Solution
from typing import List
def maximize_revenue(T: int, duration: List[int], revenue: List[int]) -> int:
best_index = 0
best_key = None
for i, (d, r) in enumerate(zip(duration, revenue)):
total = (T // d) * r
remaining = T % d
key = (total, -i, -remaining, -d)
if best_key is None or key > best_key:
best_key = key
best_index = i
return best_index