1648. Sell Diminishing-Valued Colored Balls Medium
1/**
2 * [1648] Sell Diminishing-Valued Colored Balls
3 *
4 * You have an inventory of different colored balls, and there is a customer that wants orders balls of any color.
5 * The customer weirdly values the colored balls. Each colored ball's value is the number of balls of that color you currently have in your inventory. For example, if you own 6 yellow balls, the customer would pay 6 for the first yellow ball. After the transaction, there are only 5 yellow balls left, so the next yellow ball is then valued at 5 (i.e., the value of the balls decreases as you sell more to the customer).
6 * You are given an integer array, inventory, where inventory[i] represents the number of balls of the i^th color that you initially own. You are also given an integer orders, which represents the total number of balls that the customer wants. You can sell the balls in any order.
7 * Return the maximum total value that you can attain after selling orders colored balls. As the answer may be too large, return it modulo 10^9 + 7.
8 *
9 * Example 1:
10 * <img alt="" src="https://assets.leetcode.com/uploads/2020/11/05/jj.gif" style="width: 480px; height: 270px;" />
11 * Input: inventory = [2,5], orders = 4
12 * Output: 14
13 * Explanation: Sell the 1st color 1 time (2) and the 2nd color 3 times (5 + 4 + 3).
14 * The maximum total value is 2 + 5 + 4 + 3 = 14.
15 *
16 * Example 2:
17 *
18 * Input: inventory = [3,5], orders = 6
19 * Output: 19
20 * Explanation: Sell the 1st color 2 times (3 + 2) and the 2nd color 4 times (5 + 4 + 3 + 2).
21 * The maximum total value is 3 + 2 + 5 + 4 + 3 + 2 = 19.
22 *
23 *
24 * Constraints:
25 *
26 * 1 <= inventory.length <= 10^5
27 * 1 <= inventory[i] <= 10^9
28 * 1 <= orders <= min(sum(inventory[i]), 10^9)
29 *
30 */
31pub struct Solution {}
32
33// problem: https://leetcode.com/problems/sell-diminishing-valued-colored-balls/
34// discuss: https://leetcode.com/problems/sell-diminishing-valued-colored-balls/discuss/?currentPage=1&orderBy=most_votes&query=
35
36// submission codes start here
37
38impl Solution {
39 pub fn max_profit(inventory: Vec<i32>, orders: i32) -> i32 {
40 0
41 }
42}
43
44// submission codes end
45
46#[cfg(test)]
47mod tests {
48 use super::*;
49
50 #[test]
51 fn test_1648() {
52 }
53}
54
Back
© 2025 bowen.ge All Rights Reserved.