Skip to main content

Maximize Warehouse Query Profit

easy
Software engineer

You are optimizing query execution for a virtual data warehouse. There are $n$ different types of queries available, each with a fixed execution time and profit. You can choose exactly one query type and run it repeatedly within a time limit of $k$ minutes.

Your goal is to select the query type that maximizes total profit. The selected query type can be executed multiple times (as many as fit within $k$ minutes). Each query type $i$ has a fixed execution time $executionTime_i$ (in minutes) and a fixed profit per execution $profit_i$.

If multiple query types yield the same maximum profit, select the one with the smallest index.

Return a list containing two integers: [max_profit, query_index]. If no query can be executed within the time limit, return [0, -1].

Implementation Details

  • The number of executions for query $i$ is calculated as $\lfloor k / executionTime_i \rfloor$.
  • Total profit for query $i$ is $executions \times profit_i$.

Example 1

Input

queries = [[3, 10], [2, 8], [5, 20]], k = 10

Output

[40, 1]

Explanation

For each query type, we calculate the total profit within 10 minutes:
- Query 0: Execution time 3. Executions = floor(10/3) = 3. Profit = 3 * 10 = 30.
- Query 1: Execution time 2. Executions = floor(10/2) = 5. Profit = 5 * 8 = 40.
- Query 2: Execution time 5. Executions = floor(10/5) = 2. Profit = 2 * 20 = 40.

Both Query 1 and Query 2 achieve the maximum profit of 40. We select index 1 because it is smaller than index 2.

Example 2

Input

queries = [[5, 7]], k = 5

Output

[7, 0]

Example 3

Input

queries = [[6, 100], [7, 200]], k = 5

Output

[0, -1]

Explanation

No query can be completed within 5 minutes as all execution times are greater than k.

Constraints

  • 1 <= queries.length <= 10⁴
  • queries[i].length == 2
  • 1 <= executionTime_i <= 10⁵
  • 1 <= profit_i <= 10⁴
  • 1 <= k <= 10⁵
  • The maximum total profit will fit in a 32-bit signed integer.

Onsite

ArrayEnumerationGreedyMath
Language
Code editor loads in the browser.

Output

Input

[[3,10],[2,8],[5,20]]
10

Expected

[40,1]

Your output

Run to see your output.