当前位置:网站首页>leetcode:2141. 同时运行 N 台电脑的最长时间【最值考虑二分】
leetcode:2141. 同时运行 N 台电脑的最长时间【最值考虑二分】
2022-07-28 18:35:00 【白速龙王的回眸】

分析
n <= batteries.length 不会出现电脑更多的情况
这里要有贪心思维
我们知道如果在固定n台电脑的情况下,我们可以判断运行x分钟是否可行
具体的手法就是每个电池取min(x, battery[i])这个就是它的有效使用时常
全部叠加起来看看和n * x的大小即可
一个是x分钟最多提供的电量 一个是x分钟需要提供的电量
然后check返回BOOl,最后用一点二分的技巧解决即可
ac code
class Solution:
def maxRunTime(self, n: int, batteries: List[int]) -> int:
# 最值问题考虑二分
# 给定分钟数,可以得到最多能运行的电脑书
# 分钟数越长,同时运行的电脑越少
m = len(batteries)
# 直接看看能不能运行mins分钟
def check(mins):
valid_mins = [min(mins, batteries[i]) for i in range(m)]
return sum(valid_mins) >= n * mins
l, r = 1, 10 ** 14 # 1 should be starter
while l + 1 < r:
mid = (l + r) // 2
if check(mid):
l = mid
else:
r = mid - 1
for ans in range(r, l - 1, -1):
if check(ans):
return ans
总结
注意细节,注意模拟,注意思维简单化
最值问题考虑二分
边栏推荐
- Tree row expression
- Nocturnal simulator settings agent cannot be saved
- The product power is greatly improved, and the new Ford Explorer is released
- Use of DDR3 (axi4) in Xilinx vivado (2) read write design
- Residual network RESNET source code analysis - pytoch version
- Raspberry pie CM4 -- using metartc3.0 to integrate ffmpeg to realize webrtc push-pull streaming
- Array out of bounds
- Usage Summary of thymeleaf
- Configure Windows Server + install MySQL database on the server + Remote Access database
- Linxu [basic instructions]
猜你喜欢

产品力大幅提升 新款福特探险者发布

产品经理访谈 | 第五代验证码的创新与背景

Classes and objects (medium)

Related concepts of multitasking programming

js网页黑白背景开关js特效

Music says

CNN convolution neural network learning process (weight update)

类与对象(中)

Residual network RESNET source code analysis - pytoch version

Clock distribution of jesd204 IP core (ultrascale Series)
随机推荐
微信公众号授权登录后报redirect_uri参数错误的问题
Solve the problem that the nocturnal simulator cannot access the Internet after setting an agent
js网页黑白背景开关js特效
DOS common commands
The product power is greatly improved, and the new Ford Explorer is released
[fasttext -- Summary notes]
Soft raid
Raspberry pie creation self start service
C语言简单实例 1
MySQL batch update data
[C language] 5000 word super detailed explanation of various operations of the sequence table
Raspberry pie 4B parsing PWM
Raspberry pie 4B deploy yolov5 Lite using ncnn
产品经理访谈 | 第五代验证码的创新与背景
Nocturnal simulator settings agent cannot be saved
Raspberry connects EC20 for PPP dialing
Maximum exchange [greedy thought & monotonic stack implementation]
LVM logical volume
字符设备驱动结构
C language - question brushing column