当前位置:网站首页>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
总结
二分找边界 + 等差数列
边栏推荐
- C# Rectangle basic usage and picture cutting
- 继承的注意事项
- RTL8762DK WDG(六)
- 简单的vim配置
- TFC CTF 2022 WEB Diamand WriteUp
- Google engineer fired for claiming AI awareness: breach of nondisclosure agreement
- Key Points Estimation and Point Instance
- SC7A20(士兰微-加速度传感器)示例
- Unity3D学习笔记10——纹理数组
- [MATLAB project combat] LDPC-BP channel coding
猜你喜欢

VPGNet

现代企业架构框架1
![[Reading Notes -> Data Analysis] 02 Data Analysis Preparation](/img/e7/258daf851746cb043f301437ee3bbe.png)
[Reading Notes -> Data Analysis] 02 Data Analysis Preparation

【Cryptography/Cryptanalysis】Cryptanalysis method based on TMTO

Blueprint: Yang Hui's Triangular Arrangement

MYSQL逻辑架构

Cmake introductory study notes

pycaret源码分析:下载数据集\Lib\site-packages\pycaret\datasets.py

Rainbow share | how to use moving targets defense technology to guard against the unknown

Kyoto University:Masaki Waga | 黑箱环境中强化学习的动态屏蔽
随机推荐
vim的基本使用-底行模式
OSF一分钟了解敏捷开发模式
Binary tree traversal non-recursive program -- using stack to simulate system stack
精心总结十三条建议,帮你创建更合适的MySQL索引
/usr/sbin/vmware-authdlauncher: error while loading shared libraries: libssl.so.1.0.2*解决办法
Luogu P3373: Segment tree
The role of /etc/resolv.conf
MYSQL查询截取优化分析
TFC CTF 2022 WEB Diamand WriteUp
WeChat applet page syntax
Thinking and Implementation of Object Cache Service
Interview Question: Implementing Deadlocks
qlib量化源码分析:qlib/qlib/contrib/model/gbdt.py
虚继承的原理
Nmap Operation Manual - Full Version
Carefully organize 16 MySQL usage specifications to reduce problems by 80% and recommend sharing with the team
Keil nRF52832下载失败
[AMEX] LGBM Optuna American Express Credit Card Fraud Contest kaggle
NIO programming
从零造键盘的键盘超级喜欢,IT人最爱