当前位置:网站首页>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
总结
二分找边界 + 等差数列
边栏推荐
- Difference between first and take(1) operators in NgRx
- Luogu P3373: 线段树
- Kyoto University:Masaki Waga | 黑箱环境中强化学习的动态屏蔽
- 一行代码解决CoreData托管对象属性变更在SwiftUI中无动画效果的问题
- 500 miles
- MVCC总结
- Binary tree traversal non-recursive program -- using stack to simulate system stack
- date命令
- Matlab / ArcGIS 处理GPM全球月均降水数据
- An open source and easy-to-use flowchart drawing tool drawio
猜你喜欢

STK8321 I2C(昇佳-加速度传感器)示例

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

推荐系统:常用评价指标总结【准确率、精确率、召回率、命中率、(归一化折损累计增益)NDCG、平均倒数排名(MRR)、ROC曲线、AUC(ROC曲线下的面积)、P-R曲线、A/B测试】

虹科分享|如何用移动目标防御技术防范未知因素

RTL8762DK 使用DebugAnalyzer(四)

Team of Professor Chen Jianyu of Tsinghua University | Contact Safety Reinforcement Learning Framework Based on Contact-rich Robot Operation

MYSQL主从复制

【Cryptography/Cryptanalysis】Cryptanalysis method based on TMTO

RTL8762DK PWM(七)

How to Design High Availability and High Performance Middleware - Homework
随机推荐
欧拉系统(euleros):升级Mysql
谷歌『云开发者速查表』;清华3D人体数据集;商汤『通用视觉框架』公开课;Web3极简入门指南;高效深度学习免费书;前沿论文 | ShowMeAI资讯日报
ROS2系列知识(4): 理解【服务】的概念
LeetCode--The problem of robbery
SC7A20(士兰微-加速度传感器)示例
Rainbow share | how to use moving targets defense technology to guard against the unknown
Named Entity Recognition - Model: BERT-MRC
cobaltstrike
Luogu P3373: Segment tree
RTL8762DK WDG(六)
[Reading Notes -> Data Analysis] 02 Data Analysis Preparation
GDB source code analysis series of articles five: dynamic library delay breakpoint implementation mechanism
[AMEX] LGBM Optuna American Express Credit Card Fraud Contest kaggle
Blueprint: Yang Hui's Triangular Arrangement
WAASAP WebClient UI页面标签的决定逻辑介绍
RTL8762DK UART(二)
sqlserver无法远程连接
In 2022, the latest eight Chongqing construction members (electrical construction workers) simulation question bank and answers
WindowInsetsControllerCompat is simple to use
OSD读取SAP CRM One Order应用日志的优化方式