当前位置:网站首页>模拟卷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,如果有错请留言,我看到了会修改的!谢谢!
边栏推荐
- opencv之图像分割
- Interpolation lookup (two methods)
- Database storage - table partition
- Required String parameter ‘XXX‘ is not present
- ESP32-ULP协处理器低功耗模式RTC GPIO中断唤醒
- Golan idea IntelliJ cannot input Chinese characters
- Redis fault handling "can't save in background: fork: cannot allocate memory“
- Un salaire annuel de 50 W Ali P8 vous montrera comment passer du test
- Gson转换实体类为json时报declares multiple JSON fields named
- NCS Chengdu Xindian interview experience
猜你喜欢

Greenplum6.x重新初始化

Data type - integer (C language)

LeetCode 715. Range 模块

How to realize sliding operation component in fast application

idea里使用module项目的一个bug

IP地址的类别

Mountaineering team (DFS)
![FPGA knowledge accumulation [6]](/img/db/c3721c3e842ddf4c1088a3f54e9f2a.jpg)
FPGA knowledge accumulation [6]

All about PDF crack, a complete solution to meet all your PDF needs

Count sort (diagram)
随机推荐
ncs成都新电面试经验
Find the original code, inverse code and complement of signed numbers [C language]
【MySQL】数据库进阶之触发器内容详解
Shell script for changing the current folder and the file date under the folder
Esp32-ulp coprocessor low power mode RTC GPIO interrupt wake up
Enterprise manager cannot connect to the database instance
Nanjing commercial housing sales enabled electronic contracts, and Junzi sign assisted in the online signing and filing of housing transactions
Teach you how to select PCB board by hand (II)
Opencv converts 16 bit image data to 8 bits and 8 to 16
Gson转换实体类为json时报declares multiple JSON fields named
opencv之图像分割
Componentspace2022, assertions, protocols, bindings, and configuration files
数字三角形模型 AcWing 1027. 方格取数
数据分析方法论与前人经验总结2【笔记干货】
Download and install orcale database11.2.0.4
Leetcode 1984. Minimum difference in student scores
Greenplum6.x搭建_安装
Greenplum6.x常用语句
[Yugong series] February 2022 U3D full stack class 007 - production and setting skybox resources
快速集成认证服务-HarmonyOS平台