Maximize Warehouse Query Profit
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 = 10Output
[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 = 5Output
[7, 0]
Example 3
Input
queries = [[6, 100], [7, 200]], k = 5Output
[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 == 21 <= executionTime_i <= 10⁵1 <= profit_i <= 10⁴1 <= k <= 10⁵- The maximum total profit will fit in a 32-bit signed integer.
Maximize Warehouse Query Profit
Overview
The problem asks us to select a single query type from a list and execute it repeatedly within a fixed time window $k$. For each query type, we calculate how many times it can fully complete within $k$ minutes and multiply that by its individual profit. Our goal is to find the maximum possible profit and the index of the query type that achieves it.
The tie-breaking rule is crucial: if two query types yield the same maximum profit, we must return the one that appeared earlier in the input list (the smallest index).
Brute Force
Since we only need to pick exactly one query type to run repeatedly, a brute-force approach involves iterating through every query type provided in the list, calculating its potential profit, and keeping track of the best one found so far.
For each query $i$ at index $idx$:
- Extract
executionTimeandprofitPerExecution. - Check if the query can be run at least once (i.e.,
executionTime <= k). - Calculate total executions:
executions = k // executionTime. - Calculate total profit:
current_profit = executions * profitPerExecution. - Update the global maximum if
current_profitis strictly greater than the previous maximum.
By using a "strictly greater than" (>) comparison, we naturally respect the requirement to keep the smallest index in case of a tie, as later indices with the same profit will not overwrite the first one recorded.
def maximizeQueryProfit(queries: list[list[int]], k: int) -> list[int]:
max_profit = 0
best_index = -1
for i in range(len(queries)):
execution_time = queries[i][0]
profit_per_run = queries[i][1]
# Check if the query can be executed at least once
if execution_time <= k:
executions = k // execution_time
total_profit = executions * profit_per_run
# If this profit is better than what we've seen, update.
# Using '>' ensures we keep the smallest index for ties.
if total_profit > max_profit:
max_profit = total_profit
best_index = i
# Special case for the first valid query found if max_profit is 0
elif max_profit == 0 and best_index == -1:
max_profit = total_profit
best_index = i
return [max_profit, best_index]
Time complexity: O(N) — We perform a single linear scan through the queries list, where N is the number of query types. Space complexity: O(1) — We only store a few integer variables regardless of the input size.
Optimal Approach
The brute force approach described above is actually the optimal approach for this problem. Since we must consider every query type at least once to ensure we haven't missed a more profitable option, we cannot achieve a time complexity better than $O(N)$.
We can refine the implementation slightly for cleanliness. By initializing max_profit to 0 and best_index to -1, we handle the "no query fits" case (Example 3) automatically.
def maximizeQueryProfit(queries: list[list[int]], k: int) -> list[int]:
max_profit = 0
best_index = -1
for i, (exec_time, profit) in enumerate(queries):
# If the query cannot even fit once, skip it
if exec_time > k:
continue
# Calculate total profit using floor division
num_executions = k // exec_time
total_profit = num_executions * profit
# Standard greedy update
# Because we iterate from index 0 upwards,
# using '>' automatically preserves the smallest index for ties.
if total_profit > max_profit:
max_profit = total_profit
best_index = i
# Handle the case where the first valid query results in 0 profit
# (though constraints say profit >= 1)
elif best_index == -1:
max_profit = total_profit
best_index = i
return [max_profit, best_index]
Time complexity: O(N) — Each of the $N$ queries is processed exactly once. Space complexity: O(1) — No additional data structures are used; we only track the current maximum and its index.
Edge Cases
- $k$ is smaller than all
executionTime_i: The loop will finish without updatingbest_index, and the function will correctly return[0, -1]. - Multiple queries have the same max profit: The
if total_profit > max_profitcondition ensures that only the first query found with that profit is stored. - Only one query type: The loop runs once and returns that query's profit if it fits within $k$.
- Large $k$ or profits: The problem notes state the max profit fits in a 32-bit signed integer, so we don't need to worry about overflow in standard Python 3.
Fast
Comments on the solution
No comments on the solution yet. Start a thread about the editorial or your own approach.
No comments yet for Maximize Warehouse Query Profit. Be the first to start the thread.
No submissions recorded for this problem yet. Use Submit in the editor — each judge run is stored here.
Output
Input
Expected
Your output
Run to see your output.