KatInterview

Select Best Single Task Type

Greedy & IntervalsAuto-graded · 20 test cases
EasyAuto-gradedGreedy & Intervalsgreedyarraysmath

Last 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):

  1. Total Revenue: Higher total revenue is preferred.
  2. Index: Smaller index $i$ is preferred.
  3. Leftover Time: Smaller remaining time ($T \bmod \text{duration}[i]$) is preferred.
  4. 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.