[LeetCode] Minimum Total Price After Applying Discounts

4014. Minimum Total Price After Applying Discounts

You are given two integer arrays prices and discounts.

The value prices[i] represents the price of the i^th item, and discounts[j] represents a discount percentage.

You may apply discounts subject to the following rules:

  • Each discount can be applied to at most one item.
  • Each item can receive at most one discount.
  • An item may also receive no discount.

If a discount of d percent is applied to an item with price p, its final price becomes (p * (100 - d)) / 100. The final price is not rounded.

Return the minimum possible sum of final prices after assigning discounts optimally. Answers within 10^-5 of the actual answer will be accepted.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15

class Solution {
public:
double minPrice(vector<int>& prices, vector<int>& discounts) {
sort(rbegin(discounts), rend(discounts));
sort(rbegin(prices), rend(prices));
long double res = 0.;
for(int i = 0; i < prices.size(); i++) {
if(discounts.size() > i) {
res += 1. * prices[i] * (100 - discounts[i]) / 100;
} else res += prices[i];
}
return res;
}
};
Author: Song Hayoung
Link: https://songhayoung.github.io/2026/09/03/PS/LeetCode/minimum-total-price-after-applying-discounts/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.