当前位置:网站首页>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
总结
二分找边界 + 等差数列
边栏推荐
- GDB 源码分析系列文章五:动态库延迟断点实现机制
- vim的基本使用概念
- Team of Professor Chen Jianyu of Tsinghua University | Contact Safety Reinforcement Learning Framework Based on Contact-rich Robot Operation
- Southern University of Science and Technology: Xiaoying Tang | AADG: Automatic Enhancement for Generalization in the Field of Retinal Image Segmentation
- When can I use PushGateway
- 精心总结十三条建议,帮你创建更合适的MySQL索引
- Rasa 3.x 学习系列- Rasa - Issues 4918 学习笔记
- NIO programming
- /etc/resolv.conf的作用
- Carefully summarize thirteen suggestions to help you create more suitable MySQL indexes
猜你喜欢

RTL8762DK 使用DebugAnalyzer(四)

RTL8762DK PWM(七)

vector的基本实现

两院院士直言:不要迷信院士

Introduction to the five data types of Redis
![[AMEX] LGBM Optuna American Express Credit Card Fraud Contest kaggle](/img/64/55af53a3d9dc1162490d613fe8a436.png)
[AMEX] LGBM Optuna American Express Credit Card Fraud Contest kaggle

清华大学陈建宇教授团队 | 基于接触丰富机器人操作的接触安全强化学习框架

Matlab / Arcgis处理nc数据

Automated machine learning pycaret: PyCaret Basic Auto Classification LightGBM

【密码学/密码分析】基于TMTO的密码分析方法
随机推荐
Automated machine learning pycaret: PyCaret Basic Auto Classification LightGBM
STK8321 I2C(昇佳-加速度传感器)示例
蓝图:杨辉三角排列
MYSQL-批量插入数据
cmake入门学习笔记
Web3.0: Building an NFT Market (1)
VPGNet
类和对象:中
二叉树遍历非递归程序 -- 使用栈模拟系统栈
WeChat applet page syntax
Rasa 3.x 学习系列- Rasa - Issues 4898 学习笔记
Matlab/Arcgis processing nc data
RTL8762DK RTC(五)
Exam preparation plan
C字符串数组反转
[Cloud Residency Co-Creation] [HCSD Big Celebrity Live Broadcast] Personally teach the secrets of interviews in big factories
MYSQL索引解析
声称AI存在意识,谷歌工程师遭解雇:违反保密协议
如何编辑epub电子书的目录
从零造键盘的键盘超级喜欢,IT人最爱