当前位置:网站首页>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
总结
二分找边界 + 等差数列
边栏推荐
猜你喜欢
![[AMEX] LGBM Optuna美国运通信用卡欺诈赛 kaggle](/img/64/55af53a3d9dc1162490d613fe8a436.png)
[AMEX] LGBM Optuna美国运通信用卡欺诈赛 kaggle

Redis五种数据类型简介

Classes and Objects: Medium

Southern University of Science and Technology: Xiaoying Tang | AADG: Automatic Enhancement for Generalization in the Field of Retinal Image Segmentation

Matlab / ArcGIS 处理GPM全球月均降水数据

Application of integrated stepper motor in UAV automatic airport
Carefully organize 16 MySQL usage specifications to reduce problems by 80% and recommend sharing with the team

VPGNet

现代企业架构框架1

Recommendation system: Summary of common evaluation indicators [accuracy rate, precision rate, recall rate, hit rate, (normalized depreciation cumulative gain) NDCG, mean reciprocal ranking (MRR), ROC
随机推荐
NIO programming
WAASAP WebClient UI页面标签的决定逻辑介绍
Blueprint: Yang Hui's Triangular Arrangement
Rasa 3.x 学习系列-使用查找表改进实体提取
VPGNet
500 miles
Compose原理-视图和数据双向绑定的原理
500 miles
Nmap 操作手册 - 完整版
NgRx 里 first 和 take(1) 操作符的区别
【MATLAB项目实战】LDPC-BP信道编码
精心总结十三条建议,帮你创建更合适的MySQL索引
WindowInsetsControllerCompat简单使用
【读书笔记->数据分析】02 数据分析准备
从零造键盘的键盘超级喜欢,IT人最爱
JQESAP系统里的胖接口Fat interface
Usage of mysql having
命名实体识别-模型:BERT-MRC
Introduction to the five data types of Redis
Notes on how to use zeno