当前位置:网站首页>leetcode: 1648. Color ball with decreasing sales value [Boundary find by two points]
leetcode: 1648. Color ball with decreasing sales value [Boundary find by two points]
2022-08-01 01:02:00 【Looking Back at the White Speed Dragon King】
分析
The idea is to find the biggest one every time
But the simulation times out
So dichotomous finds go to allnumthe number of balls needed
This number of balls cannot be exceededorders
Otherwise more balls
Then the extra ball is countednum的贡献
The rest of the arithmetic progressions can be summed
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都变成numnumber of balls required
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
总结
二分找边界 + 等差数列
边栏推荐
- Southern University of Science and Technology: Xiaoying Tang | AADG: Automatic Enhancement for Generalization in the Field of Retinal Image Segmentation
- Xinao Learning Plan The Road to Informatics Competition (2022.07.31)
- 【MATLAB项目实战】LDPC-BP信道编码
- STK8321 I2C(昇佳-加速度传感器)示例
- MYSQL逻辑架构
- Rasa 3.x 学习系列- Rasa - Issues 4918 学习笔记
- Rasa 3.x 学习系列- Rasa - Issues 4898 学习笔记
- Rasa 3.x Study Series - Rasa - Issues 4918 Study Notes
- Carefully summarize thirteen suggestions to help you create more suitable MySQL indexes
- Rasa 3.x 学习系列-使用查找表改进实体提取
猜你喜欢
随机推荐
一体化步进电机在无人机自动机场的应用
IPD流程专业术语
南方科技大学:Xiaoying Tang | AADG:视网膜图像分割领域泛化的自动增强
date命令
Carefully organize 16 MySQL usage specifications to reduce problems by 80% and recommend sharing with the team
Likou Binary Tree
Compose principle - the view and the principle of two-way data binding
力扣2326、197
ECCV2022 Workshop | Multi-Object Tracking and Segmentation in Complex Environments
精心总结十三条建议,帮你创建更合适的MySQL索引
[Cloud Residency Co-Creation] [HCSD Big Celebrity Live Broadcast] Personally teach the secrets of interviews in big factories
MYSQL二阶段提交
自动化机器学习pycaret: PyCaret Basic Auto Classification LightGBM
Web3.0:构建 NFT 市场(一)
TFC CTF 2022 WEB Diamand WriteUp
[1161. The maximum sum of elements in the layer]
WindowInsetsControllerCompat简单使用
Interview Question: Implementing Deadlocks
Nmap Operation Manual - Full Version
Qlib quantitative source analysis: qlib/qlib/contrib/model/GBDT py