Skip to content

Commit fc760d3

Browse files
committed
Add solution #1648
1 parent 3f4c568 commit fc760d3

File tree

2 files changed

+68
-1
lines changed

2 files changed

+68
-1
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# 1,447 LeetCode solutions in JavaScript
1+
# 1,448 LeetCode solutions in JavaScript
22

33
[https://leetcodejavascript.com](https://leetcodejavascript.com)
44

@@ -1270,6 +1270,7 @@
12701270
1643|[Kth Smallest Instructions](./solutions/1643-kth-smallest-instructions.js)|Hard|
12711271
1646|[Get Maximum in Generated Array](./solutions/1646-get-maximum-in-generated-array.js)|Easy|
12721272
1647|[Minimum Deletions to Make Character Frequencies Unique](./solutions/1647-minimum-deletions-to-make-character-frequencies-unique.js)|Medium|
1273+
1648|[Sell Diminishing-Valued Colored Balls](./solutions/1648-sell-diminishing-valued-colored-balls.js)|Medium|
12731274
1657|[Determine if Two Strings Are Close](./solutions/1657-determine-if-two-strings-are-close.js)|Medium|
12741275
1668|[Maximum Repeating Substring](./solutions/1668-maximum-repeating-substring.js)|Easy|
12751276
1669|[Merge In Between Linked Lists](./solutions/1669-merge-in-between-linked-lists.js)|Medium|
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
/**
2+
* 1648. Sell Diminishing-Valued Colored Balls
3+
* https://leetcode.com/problems/sell-diminishing-valued-colored-balls/
4+
* Difficulty: Medium
5+
*
6+
* You have an inventory of different colored balls, and there is a customer that wants orders
7+
* balls of any color.
8+
*
9+
* The customer weirdly values the colored balls. Each colored ball's value is the number of balls
10+
* of that color you currently have in your inventory. For example, if you own 6 yellow balls,
11+
* the customer would pay 6 for the first yellow ball. After the transaction, there are only 5
12+
* yellow balls left, so the next yellow ball is then valued at 5 (i.e., the value of the balls
13+
* decreases as you sell more to the customer).
14+
*
15+
* You are given an integer array, inventory, where inventory[i] represents the number of balls of
16+
* the ith color that you initially own. You are also given an integer orders, which represents
17+
* the total number of balls that the customer wants. You can sell the balls in any order.
18+
*
19+
* Return the maximum total value that you can attain after selling orders colored balls. As the
20+
* answer may be too large, return it modulo 109 + 7.
21+
*/
22+
23+
/**
24+
* @param {number[]} inventory
25+
* @param {number} orders
26+
* @return {number}
27+
*/
28+
var maxProfit = function(inventory, orders) {
29+
const MOD = 1e9 + 7;
30+
inventory.sort((a, b) => b - a);
31+
inventory.push(0);
32+
33+
let totalProfit = 0;
34+
let currentColor = 0;
35+
let ballsSold = 0;
36+
37+
while (ballsSold < orders) {
38+
const currentCount = inventory[currentColor];
39+
const nextCount = inventory[currentColor + 1];
40+
const colors = currentColor + 1;
41+
const ballsToSell = Math.min(
42+
orders - ballsSold,
43+
colors * (currentCount - nextCount)
44+
);
45+
46+
const fullSets = Math.floor(ballsToSell / colors);
47+
const remainder = ballsToSell % colors;
48+
49+
if (fullSets > 0) {
50+
const endValue = currentCount - fullSets + 1;
51+
let sequenceSum = (BigInt(colors) * BigInt(fullSets) % BigInt(MOD))
52+
* (BigInt(currentCount) + BigInt(endValue)) % BigInt(MOD);
53+
sequenceSum = (sequenceSum * BigInt(500000004)) % BigInt(MOD);
54+
totalProfit = (totalProfit + Number(sequenceSum)) % MOD;
55+
}
56+
57+
if (remainder > 0) {
58+
totalProfit = (totalProfit + (remainder * (currentCount - fullSets)) % MOD) % MOD;
59+
}
60+
61+
ballsSold += ballsToSell;
62+
currentColor++;
63+
}
64+
65+
return totalProfit;
66+
};

0 commit comments

Comments
 (0)