当前位置:网站首页>模拟卷Leetcode【普通】1705. 吃苹果的最大数目
模拟卷Leetcode【普通】1705. 吃苹果的最大数目
2022-07-07 06:18:00 【邂逅模拟卷】
1705. 吃苹果的最大数目
有一棵特殊的苹果树,一连 n 天,每天都可以长出若干个苹果。在第 i 天,树上会长出 apples[i] 个苹果,这些苹果将会在 days[i] 天后(也就是说,第 i + days[i] 天时)腐烂,变得无法食用。也可能有那么几天,树上不会长出新的苹果,此时用 apples[i] == 0 且 days[i] == 0 表示。
你打算每天 最多 吃一个苹果来保证营养均衡。注意,你可以在这 n 天之后继续吃苹果。
给你两个长度为 n 的整数数组 days 和 apples ,返回你可以吃掉的苹果的最大数目。
示例 1:
输入:apples = [1,2,3,5,2], days = [3,2,1,4,2]
输出:7
解释:你可以吃掉 7 个苹果:
- 第一天,你吃掉第一天长出来的苹果。
- 第二天,你吃掉一个第二天长出来的苹果。
- 第三天,你吃掉一个第二天长出来的苹果。过了这一天,第三天长出来的苹果就已经腐烂了。
- 第四天到第七天,你吃的都是第四天长出来的苹果。
示例 2:
输入:apples = [3,0,0,0,0,2], days = [3,0,0,0,0,2]
输出:5
解释:你可以吃掉 5 个苹果:
- 第一天到第三天,你吃的都是第一天长出来的苹果。
- 第四天和第五天不吃苹果。
- 第六天和第七天,你吃的都是第六天长出来的苹果。
提示:
apples.length == n
days.length == n
1 <= n <= 2 * 104
0 <= apples[i], days[i] <= 2 * 104
只有在 apples[i] = 0 时,days[i] = 0 才成立
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/maximum-number-of-eaten-apples
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
代码:
import heapq
from leetcode_python.utils import *
class Solution:
def __init__(self):
pass
def eatenApples(self, apples: List[int], days: List[int]) -> int:
remains = []
index = res = 0
length = len(days)
while remains or index<length:
while remains and remains[0][0]<=index:heapq.heappop(remains)
if index<length and apples[index]:heapq.heappush(remains,[days[index]+index,apples[index]])
if remains:
if remains[0][1]>1:remains[0][1]-=1
else:heapq.heappop(remains)
res+=1
index+=1
return res
def test(data_test):
s = Solution()
data = data_test # normal
# data = [list2node(data_test[0])] # list转node
return s.eatenApples(*data)
def test_obj(data_test):
result = [None]
obj = Solution(*data_test[1][0])
for fun, data in zip(data_test[0][1::], data_test[1][1::]):
if data:
res = obj.__getattribute__(fun)(*data)
else:
res = obj.__getattribute__(fun)()
result.append(res)
return result
if __name__ == '__main__':
datas = [
[[1,2,3,5,2],[3,2,1,4,2]],
]
for data_test in datas:
t0 = time.time()
print('-' * 50)
print('input:', data_test)
print('output:', test(data_test))
print(f'use time:{
time.time() - t0}s')
备注:
GitHub:https://github.com/monijuan/leetcode_python
CSDN汇总:模拟卷Leetcode 题解汇总_卷子的博客-CSDN博客
可以加QQ群交流:1092754609
leetcode_python.utils详见汇总页说明
先刷的题,之后用脚本生成的blog,如果有错请留言,我看到了会修改的!谢谢!
边栏推荐
猜你喜欢

Arm GIC (IV) GIC V3 register class analysis notes.
![[Yugong series] February 2022 U3D full stack class 006 unity toolbar](/img/2e/3a7d71a0b5a6aff294a0bd2f8515f8.jpg)
[Yugong series] February 2022 U3D full stack class 006 unity toolbar

What is the method of manual wiring in PCB design in 22protel DXP_ Chengdu electromechanical Development Undertaking

最长上升子序列模型 AcWing 1017. 怪盗基德的滑翔翼

Interpolation lookup (two methods)

LeetCode 736. Lisp 语法解析

Greenplum6.x常用语句

調用華為遊戲多媒體服務的創建引擎接口返回錯誤碼1002,錯誤信息:the params is error

How to integrate app linking services in harmonyos applications

【踩坑】nacos注册一直连接localhost:8848,no available server
随机推荐
[Chongqing Guangdong education] accounting reference materials of Nanjing University of Information Engineering
为不同类型设备构建应用的三大更新 | 2022 I/O 重点回顾
求有符号数的原码、反码和补码【C语言】
channel. Detailed explanation of queuedeclare parameters
平台化,强链补链的一个支点
A bug using module project in idea
Upload an e-office V9 arbitrary file [vulnerability recurrence practice]
登山小分队(dfs)
cmake命令行使用
MySQL partition explanation and operation statement
All about PDF crack, a complete solution to meet all your PDF needs
Composer change domestic image
You should use Google related products with caution
更改当前文件夹及文件夹下文件日期shell脚本
[Yu Yue education] basic reference materials of electrical and electronic technology of Nanjing Institute of information technology
指针进阶,字符串函数
Required String parameter ‘XXX‘ is not present
Input and output of floating point data (C language)
9c09730c0eea36d495c3ff6efe3708d8
测试踩坑 - 当已有接口(或数据库表中)新增字段时,都需要注意哪些测试点?