当前位置:网站首页>leetcode:1760. The least number of goal balls in the bag [the most valuable value + the scoreboard]
leetcode:1760. The least number of goal balls in the bag [the most valuable value + the scoreboard]
2022-07-18 12:54:00 【White speed Dragon King's review】

analysis
The best value can be considered as two points
Find monotonicity !!!
How to find ?
Let's analyze
If the maximum value of a single bag ball is larger , Is it true that the smaller the number of operations ?
conversely , The smaller the maximum , Is it the bigger the operation ;
We build a function that can calculate the minimum number of operations by giving the maximum value of a single bag
Then we ask for the minimum and maximum
So we judge cal_ops(mid)
If this value is less than or equal to maxOps, It indicates that the maximum value can be smaller , therefore r take mid, Can succeed
conversely , If it is greater than maxOps, Too many operands , Indicate that the maximum value should be increased , here mid It's cool
So we need to l = mid + 1
Finally, it returns the minimum value, so it returns l
ac code
class Solution:
def minimumSize(self, nums: List[int], maxOperations: int) -> int:
# The minimum of the maximum , The maximum value of the minimum value is divided into two
# The smaller the maximum , The more operations ; The greater the maximum , The smaller the operation
# Classic dichotomy
# The calculated maximum value is x Operands required when
def cal_ops(x):
'''x: Maximum value of a single bag '''
res = 0
for num in nums:
if num > x:
# Note that there -1, for example 6 change 3 Only 1 Time
res += (num - 1) // x
return res
l, r = 1, 10 ** 9
# The smallest can be successful and return
if cal_ops(l) <= maxOperations:
return l
# Classic dichotomy
while l < r:
mid = l + (r - l) // 2
if cal_ops(mid) <= maxOperations:
# You can make the maximum value smaller
r = mid
else:
# The maximum value must be made larger
l = mid + 1 # because mid It's cool
return l # The returned is the smallest , So it is l
summary
The best value is considered as two points
Generally, a function has some monotonicity
Then two points x, Get the closest and most valuable y
边栏推荐
- AIRIOT答疑第4期|如何使用数据分析引擎?
- 自动追番工具BGmi
- Detailed explanation and examples of anti shake throttling
- Unity screenshot function and let the UI display
- MySQL error 1142 - select command denied to user 'dev' @ 'localhost' for table 'user' (resolved)
- JS数组最全方法解读!!!
- Personal safety protection order
- Analysis of container health inspection
- Is free knowledge not worth paying attention to?
- [QT入门篇]三大窗口类介绍
猜你喜欢
随机推荐
Unity screenshot function and let the UI display
0714 1 pm, review,
Analysis of Large Integer Decomposition
9. Conception du module Px4: introduction au mode de vol Px4
Maximum return alternative method
6000 Digital Collections Online seconds empty! "National treasure" digital collections look like this
记录根文件系统镜像img制作
py篇之字典系列
Open source data set - flower classification data set
揭秘MetaMask的起源:成为入门Web3文化的工具集
Py chapter Dictionary Series
P1765 mobile phone [getting started]
C语言经典实例:11-20例:求二维数组最大最小值、数组求素数、编制万年历、数组元素排序、进制数的转换进制数的转换、验证哥德巴赫猜想找出次大值、使用结构体输出学生成绩、重组数组
PX4模块设计之九:PX4飞行模式简介
MySQL基礎——新增與進階查詢
Fraudulent alimony can claim the paid alimony and mental damage solace
Programming old driver takes you to play with completable future asynchronous programming
Gm3234 Yin Yang
Recommendation on quick impact methods for sophomores who don't fail (data structure + group count + operating system + algorithm + database + network count)
[QT入门篇]三大窗口类介绍








