当前位置:网站首页>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
总结
注意细节,注意模拟,注意思维简单化
最值问题考虑二分
边栏推荐
- Using typedef in C language to change the name of data type
- Linxu [basic instructions]
- Use of DDR3 (axi4) in Xilinx vivado (2) read write design
- User, user group related operations
- 同质化代币与 NFT 结合,如何使治理结构设计更灵活?
- Raspberry pie 4B ffmpeg RTMP streaming
- LeetCode-297-二叉树的序列化与反序列化
- About IP address
- Maximum exchange [greedy thought & monotonic stack implementation]
- Leetcode-297 serialization and deserialization of binary tree
猜你喜欢

Read JSON configuration file to realize data-driven testing

Raspberry pie 4B parsing PWM

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

WUST-CTF2021-re校赛wp

Why is customer support important to SaaS?

DOS common commands
![[C language] guessing numbers game](/img/ac/81a82404618487861b67e35f18d13f.png)
[C language] guessing numbers game

读取json配置文件,实现数据驱动测试
![[POC - proof of concept]](/img/57/0916e3711b27e2debfbdb9c9cb9713.png)
[POC - proof of concept]
![Teach you how to draw a map with ArcGIS [thermal map]](/img/16/993da4678667884a98e1d82db37d69.png)
Teach you how to draw a map with ArcGIS [thermal map]
随机推荐
Raspberry pie 4B uses MNN to deploy yolov5 Lite
类与对象(中)
Linux Installation MySQL (pit filling version)
产品经理访谈 | 第五代验证码的创新与背景
[C语言刷题篇]链表运用讲解
C语言简单实例 1
一碰撞就自燃,谁来关心电池安全?上汽通用有话说
C language - data type
[task02: SQL basic query and sorting]
Solve the kangaroo crossing problem (DP)
MySQL startup error 1607 unexpected process termination
Other IPS cannot connect to the local redis problem solving and redis installation
How can Plato obtain premium income through elephant swap in a bear market?
Voice controlled robot based on ROS (II): implementation of upper computer
CNN convolutional neural network structure
Classes and objects (medium)
C language - pointer
New fruit naming (DP is similar to the longest common subsequence)
Gru neural network
Dsactf July re