You are given two integer arrays prices and ratings of length n, and an integer budget. Find the index of the item that maximizes the value-for-money ratio ratings[i] / prices[i] among all items whose price does not exceed budget.
If several items share the maximum value-for-money ratio, return the one with the lowest index. If no item is within the budget, return -1.
Function Signature
def best_value_item(prices: list[int], ratings: list[int], budget: int) -> int:
Rules
-
The ratio is exact division, so
120 / 20
is 6 and
90 / 30
is 3. Integer division must not be used.
-
An item is within the budget when
prices[i] <= budget
. An item priced exactly at the budget counts.
-
Indices are 0-based. Ties on the ratio go to the lowest index, and the answer is
-1
when no item is affordable.
Constraints
-
1 <= n <= 10^5
-
1 <= prices[i] <= 10^6
-
1 <= ratings[i] <= 10^6
-
1 <= budget <= 10^6
-
len(prices) == len(ratings)
Examples
Input: prices = [10, 20, 30, 40], ratings = [50, 120, 90, 200], budget = 35
Output: 1
The items within budget are index 0 (ratio 5.0), index 1 (ratio 6.0) and index 2 (ratio 3.0). Index 3 costs 40 and is excluded, so the maximum ratio is 6.0 at index 1.
Input: prices = [100, 200, 50], ratings = [300, 400, 150], budget = 50
Output: 2
Only the item at index 2 (price 50) is within the budget. Its ratio is 150 / 50 = 3.0.
Input: prices = [4, 2, 6], ratings = [8, 4, 30], budget = 5
Output: 0
Index 2 has the highest ratio (5.0) but costs more than the budget. Indices 0 and 1 both have ratio 2.0, and the tie goes to the lower index.