-
Notifications
You must be signed in to change notification settings - Fork 32
/
Copy path1648-sell-diminishing-valued-colored-balls.js
66 lines (58 loc) · 2.31 KB
/
1648-sell-diminishing-valued-colored-balls.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
/**
* 1648. Sell Diminishing-Valued Colored Balls
* https://leetcode.com/problems/sell-diminishing-valued-colored-balls/
* Difficulty: Medium
*
* You have an inventory of different colored balls, and there is a customer that wants orders
* balls of any color.
*
* 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).
*
* You are given an integer array, inventory, where inventory[i] represents the number of balls of
* the ith 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.
*
* Return the maximum total value that you can attain after selling orders colored balls. As the
* answer may be too large, return it modulo 109 + 7.
*/
/**
* @param {number[]} inventory
* @param {number} orders
* @return {number}
*/
var maxProfit = function(inventory, orders) {
const MOD = 1e9 + 7;
inventory.sort((a, b) => b - a);
inventory.push(0);
let totalProfit = 0;
let currentColor = 0;
let ballsSold = 0;
while (ballsSold < orders) {
const currentCount = inventory[currentColor];
const nextCount = inventory[currentColor + 1];
const colors = currentColor + 1;
const ballsToSell = Math.min(
orders - ballsSold,
colors * (currentCount - nextCount)
);
const fullSets = Math.floor(ballsToSell / colors);
const remainder = ballsToSell % colors;
if (fullSets > 0) {
const endValue = currentCount - fullSets + 1;
let sequenceSum = (BigInt(colors) * BigInt(fullSets) % BigInt(MOD))
* (BigInt(currentCount) + BigInt(endValue)) % BigInt(MOD);
sequenceSum = (sequenceSum * BigInt(500000004)) % BigInt(MOD);
totalProfit = (totalProfit + Number(sequenceSum)) % MOD;
}
if (remainder > 0) {
totalProfit = (totalProfit + (remainder * (currentCount - fullSets)) % MOD) % MOD;
}
ballsSold += ballsToSell;
currentColor++;
}
return totalProfit;
};