当前位置:网站首页>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
总结
二分找边界 + 等差数列
边栏推荐
猜你喜欢

蓝图:杨辉三角排列

RTL8762DK PWM(七)

精心总结十三条建议,帮你创建更合适的MySQL索引

Super like the keyboard made from zero, IT people love it
![[微服务]分布式事务解决方案-Seata](/img/a8/fc6c24e4d42dfb635bad786cc02164.png)
[微服务]分布式事务解决方案-Seata

声称AI存在意识,谷歌工程师遭解雇:违反保密协议

MYSQL查询截取优化分析

类和对象:上

NIO programming

Team of Professor Chen Jianyu of Tsinghua University | Contact Safety Reinforcement Learning Framework Based on Contact-rich Robot Operation
随机推荐
The principle of virtual inheritance
[Reading Notes -> Data Analysis] 02 Data Analysis Preparation
Blueprint: Yang Hui's Triangular Arrangement
Carefully summarize thirteen suggestions to help you create more suitable MySQL indexes
Luogu P3373: Segment tree
RTL8762DK RTC(五)
推荐系统:常用评价指标总结【准确率、精确率、召回率、命中率、(归一化折损累计增益)NDCG、平均倒数排名(MRR)、ROC曲线、AUC(ROC曲线下的面积)、P-R曲线、A/B测试】
To help the construction of digital government, the three parties of China Science and Technology build a domain name security system
一行代码解决CoreData托管对象属性变更在SwiftUI中无动画效果的问题
WindowInsetsControllerCompat简单使用
Matlab / ArcGIS 处理GPM全球月均降水数据
MYSQL逻辑架构
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
开源好用的 流程图绘制工具 drawio
mysql having的用法
Named Entity Recognition - Model: BERT-MRC
GDB source code analysis series of articles five: dynamic library delay breakpoint implementation mechanism
自动化机器学习pycaret: PyCaret Basic Auto Classification LightGBM
2022年最新重庆建筑八大员(电气施工员)模拟题库及答案
IPD流程专业术语