当前位置:网站首页>模拟卷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,如果有错请留言,我看到了会修改的!谢谢!
边栏推荐
- Frequently Asked Coding Problems
- 如何统计项目代码行数
- Analysis of abnormal channel number information before and after AGC re signature service
- Greenplum 6.x common statements
- POJ - 3784 running medium
- Greenplum 6.x reinitialization
- Gson converts the entity class to JSON times declare multiple JSON fields named
- [南京大学]-[软件分析]课程学习笔记(一)-introduction
- 平台化,强链补链的一个支点
- opencv 将16位图像数据转为8位、8转16
猜你喜欢
随机推荐
Greenplum 6.x build_ install
[Yu Yue education] basic reference materials of electrical and electronic technology of Nanjing Institute of information technology
調用華為遊戲多媒體服務的創建引擎接口返回錯誤碼1002,錯誤信息:the params is error
Mock.js用法详解
Greenplum6.x-版本变化记录-常用手册
[Nanjing University] - [software analysis] course learning notes (I) -introduction
[Yu Yue education] C language programming reference of Zhongbei College of Nanjing Normal University
【踩坑】nacos注册一直连接localhost:8848,no available server
What is the method of manual wiring in PCB design in 22protel DXP_ Chengdu electromechanical Development Undertaking
What are the advantages of commas in conditional statements- What is the advantage of commas in a conditional statement?
Test pits - what test points should be paid attention to when adding fields to existing interfaces (or database tables)?
Lenovo hybrid cloud Lenovo xcloud: 4 major product lines +it service portal
GoLand set goproxy
AVL balanced binary search tree
如何统计项目代码行数
Other 7 features of TCP [sliding window mechanism ▲]
Required String parameter ‘XXX‘ is not present
南京商品房买卖启用电子合同,君子签助力房屋交易在线网签备案
Greenplum6.x搭建_环境配置
ESP32-ULP协处理器低功耗模式RTC GPIO中断唤醒









