Minimum Relative Loss After Buying Chocolates Problem
Description
LeetCode Problem 2819.
You are given an integer array prices, which shows the chocolate prices and a 2D integer array queries, where queries[i] = [k_i, m_i].
Alice and Bob went to buy some chocolates, and Alice suggested a way to pay for them, and Bob agreed.
The terms for each query are as follows:
- If the price of a chocolate is less than or equal to
k_i, Bob pays for it. - Otherwise, Bob pays
k_iof it, and Alice pays the rest.
Bob wants to select exactly m_i chocolates such that his relative loss is minimized, more formally, if, in total, Alice has paid a_i and Bob has paid b_i, Bob wants to minimize b_i - a_i.
Return an integer array ans where ans[i] is Bob's minimum relative loss possible for queries[i].
Example 1:
Input: prices = [1,9,22,10,19], queries = [[18,4],[5,2]]
Output: [34,-21]
Explanation: For the 1^st query Bob selects the chocolates with prices [1,9,10,22]. He pays 1 + 9 + 10 + 18 = 38 and Alice pays 0 + 0 + 0 + 4 = 4. So Bob's relative loss is 38 - 4 = 34.
For the 2^nd query Bob selects the chocolates with prices [19,22]. He pays 5 + 5 = 10 and Alice pays 14 + 17 = 31. So Bob's relative loss is 10 - 31 = -21.
It can be shown that these are the minimum possible relative losses.
Example 2:
Input: prices = [1,5,4,3,7,11,9], queries = [[5,4],[5,7],[7,3],[4,5]]
Output: [4,16,7,1]
Explanation: For the 1^st query Bob selects the chocolates with prices [1,3,9,11]. He pays 1 + 3 + 5 + 5 = 14 and Alice pays 0 + 0 + 4 + 6 = 10. So Bob's relative loss is 14 - 10 = 4.
For the 2^nd query Bob has to select all the chocolates. He pays 1 + 5 + 4 + 3 + 5 + 5 + 5 = 28 and Alice pays 0 + 0 + 0 + 0 + 2 + 6 + 4 = 12. So Bob's relative loss is 28 - 12 = 16.
For the 3^rd query Bob selects the chocolates with prices [1,3,11] and he pays 1 + 3 + 7 = 11 and Alice pays 0 + 0 + 4 = 4. So Bob's relative loss is 11 - 4 = 7.
For the 4^th query Bob selects the chocolates with prices [1,3,7,9,11] and he pays 1 + 3 + 4 + 4 + 4 = 16 and Alice pays 0 + 0 + 3 + 5 + 7 = 15. So Bob's relative loss is 16 - 15 = 1.
It can be shown that these are the minimum possible relative losses.
Example 3:
Input: prices = [5,6,7], queries = [[10,1],[5,3],[3,3]]
Output: [5,12,0]
Explanation: For the 1^st query Bob selects the chocolate with price 5 and he pays 5 and Alice pays 0. So Bob's relative loss is 5 - 0 = 5.
For the 2^nd query Bob has to select all the chocolates. He pays 5 + 5 + 5 = 15 and Alice pays 0 + 1 + 2 = 3. So Bob's relative loss is 15 - 3 = 12.
For the 3^rd query Bob has to select all the chocolates. He pays 3 + 3 + 3 = 9 and Alice pays 2 + 3 + 4 = 9. So Bob's relative loss is 9 - 9 = 0.
It can be shown that these are the minimum possible relative losses.
Constraints:
1 <= prices.length == n <= 10^51 <= prices[i] <= 10^91 <= queries.length <= 10^5queries[i].length == 21 <= k_i <= 10^91 <= m_i <= n
Solution
Approach: Sorting + binary search + prefix sums
For a chocolate of price p and a budget k for Alice: if p <= k, Bob pays everything and his relative loss is p, so cheaper is better. If p > k, Alice pays k and Bob pays p - k, so the loss is k - (p - k) = 2k - p, and more expensive is better.
So after sorting, Bob takes some cheapest chocolates from the left and the rest from the most expensive ones on the right. For a query (k, m), binary search the number l of chocolates taken from the left (at most m and only among prices <= k): compare prices[mid] with 2k - prices[n - (m - mid)]. With prefix sums s, the loss is s[l] + 2k(m - l) - (s[n] - s[n - (m - l)]).
Complexity: Time O((n + q) log n), space O(n).
from bisect import bisect_right
from itertools import accumulate
class Solution:
def minimumRelativeLosses(
self, prices: List[int], queries: List[List[int]]
) -> List[int]:
def f(k: int, m: int) -> int:
l, r = 0, min(m, bisect_right(prices, k))
while l < r:
mid = (l + r) >> 1
right = m - mid
if prices[mid] < 2 * k - prices[n - right]:
l = mid + 1
else:
r = mid
return l
prices.sort()
s = list(accumulate(prices, initial=0))
ans = []
n = len(prices)
for k, m in queries:
l = f(k, m)
r = m - l
loss = s[l] + 2 * k * r - (s[n] - s[n - r])
ans.append(loss)
return ans