[leetcode] 1648. Sell Diminishing-Valued Colored Balls

网友投稿 776 2022-10-01

[leetcode] 1648. Sell Diminishing-Valued Colored Balls

[leetcode] 1648. Sell Diminishing-Valued Colored Balls

Description

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.

Example 1:

Input: inventory = [2,5], orders = 4Output: 14Explanation: Sell the 1st color 1 time (2) and the 2nd color 3 times (5 + 4 + 3).The maximum total value is 2 + 5 + 4 + 3 = 14.

Example 2:

Input: inventory = [3,5], orders = 6Output: 19Explanation: Sell the 1st color 2 times (3 + 2) and the 2nd color 4 times (5 + 4 + 3 + 2).The maximum total value is 3 + 2 + 5 + 4 + 3 + 2 = 19.

Example 3:

Input: inventory = [2,8,4,10,6], orders = 20Output: 110

Example 4:

Input: inventory = [1000000000], orders = 1000000000Output: 21Explanation: Sell the 1st color 1000000000 times for a total value of 500000000500000000. 500000000500000000 modulo 109 + 7 = 21.

Constraints:

1 <= inventory.length <= 1051 <= inventory[i] <= 1091 <= orders <= min(sum(inventory[i]), 109)

分析

题目的意思是:这道题给出了很多不同颜色的球,然后每卖出球的价值和它本身的数量有关。思路很直接,贪心的算法,每次取出数量多的球就能达到价值最大化,如果按照这个规则暴力破解的话,网上的人说的是超时,我也就没尝试。

换一种思路,如果我知道每个球最多卖多少次的话就容易求出总的最大价值了,所以可以使用二分法找到卖出球数量的边界,二分的时候要保证orders大于等于0,把边界向左移动,即r=mid-1;否则的话需要把边界向右移动,即l=mid+1。求出边界l以后,我们就可用等差数列求和公式算出每个球卖了的价值,a1+a2+…+an=(a1+an)*n//2,orders进行更新如果orders还大于0,说明还可以再卖orders个球,就把l边界的数量的球取orders个卖掉就能达到最大价值了

代码

class Solution: def isValid(self,inventory,k,orders): for num in inventory: if(num>k): orders-=num-k if(orders<0): return False return orders>=0 def maxProfit(self, inventory: List[int], orders: int) -> int: mod=10**9+7 l=0 r=max(inventory) while(l<=r): mid=l+(r-l)//2 if(self.isValid(inventory,mid,orders)): r=mid-1 else: l=mid+1 res=0 for num in inventory: if(num>l): a1=num an=l+1 n=num-l res+=((a1+an)*n//2)%mod orders-=n if(orders>0): res+=l*orders res%=mod

参考文献

​​[leetcode题解] 第1648题Sell Diminishing Valued Colored Balls​​

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:Springboot实例讲解实现专业材料认证管理系统流程
下一篇:微信聊天中的拍了拍是什么意思?(微信显示拍了拍是什么意思)
相关文章

 发表评论

暂时没有评论,来抢沙发吧~