当前位置:网站首页>leetcode:1648. 销售价值减少的颜色球【二分找边界】
leetcode:1648. 销售价值减少的颜色球【二分找边界】
2022-08-01 00:55:00 【白速龙王的回眸】
分析
思路无非每次找最大的即可
但是模拟超时
因此二分找到去到全都是num时候需要的球数
这个球数不能超过orders
否则就要更多球
然后多出来的那部分球就算是num的贡献
其余的等差数列求和即可
ac code
class Solution:
def maxProfit(self, inventory: List[int], orders: int) -> int:
MOD = 10 ** 9 + 7
inventory.sort()
n = len(inventory)
def get_balls(num):
# 所有大于num都变成num所需要的球数
idx = bisect_right(inventory, num) # 第一个大于num的
cnt = 0
for i in range(idx, n):
cnt += inventory[i] - num
return cnt
l, r = 0, inventory[-1]
while l + 1 < r:
mid = (l + r) // 2
if get_balls(mid) <= orders:
r = mid
else:
l = mid + 1
ball1 = get_balls(r)
ball2 = get_balls(l)
ballFinal, numFinal = -1, -1
if ball2 < orders:
ballFinal, numFinal = ball2, l
else:
ballFinal, numFinal = ball1, r
rest = orders - ballFinal
ans = 0
ans = numFinal * rest # rest
idx = bisect_right(inventory, numFinal)
for i in range(idx, n):
ans += ((numFinal + 1 + inventory[i]) * (inventory[i] - numFinal) // 2)
return ans % MOD
总结
二分找边界 + 等差数列
边栏推荐
- Carefully summarize thirteen suggestions to help you create more suitable MySQL indexes
- RTL8762DK 使用DebugAnalyzer(四)
- cmake入门学习笔记
- MYSQL-批量插入数据
- Nmap Operation Manual - Full Version
- Rasa 3.x Learning Series - Using Lookup Tables to Improve Entity Extraction
- Web3.0: Building an NFT Market (1)
- /usr/local/bin和/usr/bin的区别
- 一体化步进电机在无人机自动机场的应用
- [MATLAB project combat] LDPC-BP channel coding
猜你喜欢
NIO programming
VPGNet
南方科技大学:Xiaoying Tang | AADG:视网膜图像分割领域泛化的自动增强
Carefully summarize thirteen suggestions to help you create more suitable MySQL indexes
Google "Cloud Developer Quick Checklist"; Tsinghua 3D Human Body Dataset; SenseTime "Universal Vision Framework" open class; Web3 Minimalist Getting Started Guide; Free Books for Efficient Deep Learni
STK8321 I2C(昇佳-加速度传感器)示例
/etc/sysconfig/network-scripts configure the network card
你需要知道的 TCP 四次挥手
cobaltstrike
JVM面试题总结(持续更新中)
随机推荐
IPD process terminology
ECCV2022 Workshop | 复杂环境中的多目标跟踪和分割
虚继承的原理
LeetCode--The problem of robbery
Kyoto University:Masaki Waga | 黑箱环境中强化学习的动态屏蔽
thymeleaf迭代map集合
声称AI存在意识,谷歌工程师遭解雇:违反保密协议
MYSQL查询截取优化分析
When can I use PushGateway
Keil nRF52832下载失败
Matlab/ArcGIS processing GPM global monthly precipitation data
Notes on how to use zeno
[MATLAB project combat] LDPC-BP channel coding
Interview Question: Implementing Deadlocks
值传递还是引用传递(By Value or By Reference)
Rasa 3.x Study Series - Rasa - Issues 4918 Study Notes
An open source and easy-to-use flowchart drawing tool drawio
Rasa 3.x Study Series - Rasa - Issues 4898 Study Notes
【MATLAB项目实战】LDPC-BP信道编码
WindowInsetsControllerCompat简单使用