1648. Sell Diminishing-Valued Colored Balls 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.
Solution
-
Count the number of occurrences of the characters in the string.
-
Note the visited number.
-
Find the previous unvisited number.
If the number if 0, it means that we have removed the characters. Thus we should NOT add
0
tovisited
.
def minDeletions(self, s):
"""
:type s: str
:rtype: int
"""
dic = collections.Counter(s)
res = 0
visited = set()
# print(unvisited)
for k, v in dic.items():
while v in visited:
v -= 1
res += 1
if v != 0:
visited.add(v)
# print(k, v, unvisited)
return res