当前位置:网站首页>Simulation volume leetcode [general] 1705 The maximum number of apples to eat
Simulation volume leetcode [general] 1705 The maximum number of apples to eat
2022-07-07 08:55:00 【Encounter simulation volume】
1705. The maximum number of apples to eat
There's a special apple tree , One after another n God , Several apples can grow every day . In the i God , Trees grow apples[i] An apple , These apples will be in days[i] Days later ( in other words , The first i + days[i] days ) Rot , Become inedible . Maybe for a few days , New apples don't grow on trees , Use at this time apples[i] == 0 And days[i] == 0 Express .
You're going to... Every day most Eat an apple to keep a balanced diet . Be careful , You can be here n Keep eating apples after three days .
Here are two lengths for you n Array of integers for days and apples , Return the maximum number of apples you can eat .
Example 1:
Input :apples = [1,2,3,5,2], days = [3,2,1,4,2]
Output :7
explain : You can eat 7 An apple :
- The first day , You eat the apples that grow on the first day .
- the second day , You eat an apple that grows the next day .
- On the third day , You eat an apple that grows the next day . After this day , On the third day, the apple has rotted .
- From day four to day seven , You eat apples that grow on the fourth day .
Example 2:
Input :apples = [3,0,0,0,0,2], days = [3,0,0,0,0,2]
Output :5
explain : You can eat 5 An apple :
- From day one to day three , You eat apples that grow on the first day .
- No apples for the fourth and fifth days .
- The sixth and seventh days , You eat apples that grow on the sixth day .
Tips :
apples.length == n
days.length == n
1 <= n <= 2 * 104
0 <= apples[i], days[i] <= 2 * 104
Only in apples[i] = 0 when ,days[i] = 0 It was established.
source : Power button (LeetCode)
link :https://leetcode-cn.com/problems/maximum-number-of-eaten-apples
Copyright belongs to the network . For commercial reprint, please contact the official authority , Non-commercial reprint please indicate the source .
Code :
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 turn 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')
remarks :
GitHub:https://github.com/monijuan/leetcode_python
CSDN Summary : Simulation volume Leetcode Summary of questions _ Paper blog -CSDN Blog
You can add QQ Group communication :1092754609
leetcode_python.utils See the description on the summary page for details
First brush questions , Then generated by script blog, If there is any mistake, please leave a message , I see it will be revised ! thank you !
边栏推荐
- opencv 将16位图像数据转为8位、8转16
- Interpolation lookup (two methods)
- Greenplum 6.x common statements
- opencv之图像分割
- Greenplum 6.x monitoring software setup
- MySQL partition explanation and operation statement
- Implement custom memory allocator
- 【ChaosBlade:节点磁盘填充、杀节点上指定进程、挂起节点上指定进程】
- xray的简单使用
- Personal deduction topic classification record
猜你喜欢

Greenplum 6.x reinitialization

2022-07-06 Unity核心9——3D动画

Lenovo hybrid cloud Lenovo xcloud: 4 major product lines +it service portal
![[Yugong series] February 2022 U3D full stack class 007 - production and setting skybox resources](/img/e3/3703bdace2d0ca47c1a585562dc15e.jpg)
[Yugong series] February 2022 U3D full stack class 007 - production and setting skybox resources

Greenplum 6.x common statements

The longest ascending subsequence model acwing 1017 Strange thief Kidd's glider

【MySQL】数据库进阶之触发器内容详解

NCS Chengdu New Electric interview Experience

Oracle makes it clear at one time that a field with multiple separators will be split into multiple rows, and then multiple rows and columns. Multiple separators will be split into multiple rows, and

Image segmentation in opencv
随机推荐
[Yugong series] February 2022 U3D full stack class 008 - build a galaxy scene
Tronapi wave field interface - source code without encryption - can be opened twice - interface document attached - package based on thinkphp5 - detailed guidance of the author - July 6, 2022 - Novice
Esp32-ulp coprocessor low power mode RTC GPIO interrupt wake up
ncs成都新電面試經驗
Mountaineering team (DFS)
【ChaosBlade:节点磁盘填充、杀节点上指定进程、挂起节点上指定进程】
With an annual salary of 50W, Alibaba P8 will come out in person to teach you how to advance from testing
Calf problem
Greenplum6.x搭建_安装
NCS Chengdu New Electric interview Experience
Golang etcdv3 reports an error. The attribute in grpc does not exist
How to realize sliding operation component in fast application
Greenplum 6.x reinitialization
Enterprise manager cannot connect to the database instance
指针进阶,字符串函数
PPT模板、素材下载网站(纯干货,建议收藏)
Other 7 features of TCP [sliding window mechanism ▲]
路由信息协议——RIP
Greenplum 6.x common statements
The longest ascending subsequence model acwing 1017 Strange thief Kidd's glider