本文主要是介绍Leetcode 1705. 吃苹果的最大数目(medium),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
题目
有一棵特殊的苹果树,一连 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 个苹果:
- 第一天到第三天,你吃的都是第一天长出来的苹果。
- 第四天和第五天不吃苹果。
- 第六天和第七天,你吃的都是第六天长出来的苹果。来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/maximum-number-of-eaten-apples
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
解题思路
1. 原始解法
使用一个列表food来存储信息,列表的每个元素是[i + days[i], apples[i]]
前n天:
(1). 收获苹果:当某一天的apples[i]不为0时,才向food添加元素
food = []
for i in range(n): # at day i, we first harvest applesif apples[i] != 0:food.append([i + days[i], apples[i]])
(2).对food进行排序,依据为i + days[i]:排序是为了吃最快腐烂的苹果
# then we need to sort the list based on i+days[i]
food.sort(key=lambda x: x[0])
(3).如果有苹果,则吃一个
# now we have to find out whether we have an apple to eat
if len(food) != 0:food[0][1] -= 1count += 1
(4).丢弃所有将要腐烂的苹果(以及被吃完的food[0])
# if we ate all apples from food[0], drop it
if len(food) != 0:if food[0][1] == 0:food.pop(0)# at the end of day, we need to throw away all rotten apples
if len(food) != 0:while (food[0][0] == i + 1):food.pop(0)if len(food) == 0:break
后续:
每一天吃一个苹果,并丢弃过期的。这里我使用了for循环,所以需要先确定最后一批苹果腐烂的那一天。如果前n天吃掉了所有水果,则无需处理后续部分。
# max_day represents the last day we have an apple
if len(food) > 0:max_day = food[-1][0]
else:max_day = n
吃苹果、丢苹果与前n天类似
结果:通过66/69测试用例,之后超时。原因应该是排序的部分。
完整代码:
class Solution:def eatenApples(self, apples: List[int], days: List[int]) -> int:count = 0n = len(apples)# use a list to store all the information# each element of the list has two components [i+days[i], apples[i]] food = []for i in range(n): # at day i, we first harvest applesif apples[i] != 0:food.append([i + days[i], apples[i]])# then we need to sort the list based on i+days[i]food.sort(key=lambda x: x[0])# now we have to find out whether we have an apple to eatif len(food) != 0:food[0][1] -= 1count += 1# if we ate all apples from food[0], drop itif len(food) != 0:if food[0][1] == 0:food.pop(0)# at the end of day, we need to throw away all rotten applesif len(food) != 0:while (food[0][0] == i + 1):food.pop(0)if len(food) == 0:break# max_day represents the last day we have an appleif len(food) > 0:max_day = food[-1][0]else:max_day = nfor i in range(n, max_day):# if there's no appleif len(food) == 0:break# else eat an applefood[0][1] -= 1count += 1# if we ate all apples from food[0], drop itif food[0][1] == 0:food.pop(0)# at the end of day, we need to throw away all rotten applesif len(food) != 0:while (food[0][0] == i + 1):food.pop(0)if len(food) == 0:breakreturn count
2.第一次优化
使用heapq创建一个优先队列取代列表。使用queue.PriorityQueue也可以实现目的,后者只是加入了线程方面的保护,操作仍是基于heapq。
前n天:
(1). 收获苹果
import heapq
food = []for i in range(n): # at day i, we first harvest applesif apples[i] != 0:heapq.heappush(food, [i + days[i], apples[i]])
(2).由于使用了heapq,此时我们无需再排序,直接判断能否吃一个苹果。此处另一个区别在于,我们将food[0]直接从队列中取出了,如果我们没有吃完food[0],需要将其再加入队列。
# now we have to find out whether we have an apple to eat
if len(food) != 0:count += 1temp = heapq.heappop(food)if temp[1] > 1:# if we haven't eaten all the apples from food[0]heapq.heappush(food, [temp[0], temp[1] - 1])
(3).丢弃将要腐烂的苹果
# at the end of day, we need to throw away all rotten apples
while(len(food) > 0):if food[0][0] == i + 1:heapq.heappop(food)else:break
后续:
在后续部分的处理中,由于使用了堆,导致无法通过food[-1]的方式得到最后一批苹果腐烂的时间。所以要用while循环判定是否还有苹果剩下,并用另一个变量记录日期。(也许这本身就是更好的处理方法)
# start from day n
i = n
# if there are still some apples left
while(len(food) > 0):count += 1temp = heapq.heappop(food)if temp[1] > 1:# if we haven't eaten all the apples from food[0]heapq.heappush(food, [temp[0], temp[1] - 1])# at the end of day, we need to throw away all rotten appleswhile(len(food) > 0):if food[0][0] == i + 1:heapq.heappop(food)else:breaki += 1
结果:通过全部测试用例。用时456ms,内存使用量19.3MB。
完整代码:
class Solution:def eatenApples(self, apples: List[int], days: List[int]) -> int:count = 0n = len(apples)# use a list to store all the information# each element of the list has two components [i+days[i], apples[i]] # use heapqimport heapq food = []for i in range(n):# at day i, we first harvest applesif apples[i] != 0:heapq.heappush(food, [i + days[i], apples[i]])# now we have to find out whether we have an apple to eatif len(food) != 0:count += 1temp = heapq.heappop(food)if temp[1] > 1:# if we haven't eaten all the apples from food[0]heapq.heappush(food, [temp[0], temp[1] - 1])# at the end of day, we need to throw away all rotten appleswhile(len(food) > 0):if food[0][0] == i + 1:heapq.heappop(food)else:breaki = n# if there are still some apples leftwhile(len(food) > 0):count += 1temp = heapq.heappop(food)if temp[1] > 1:# if we haven't eaten all the apples from food[0]heapq.heappush(food, [temp[0], temp[1] - 1])# at the end of day, we need to throw away all rotten appleswhile(len(food) > 0):if food[0][0] == i + 1:heapq.heappop(food)else:breaki += 1return count
3.第二次优化
通过学习题解,更改了代码的结构。主要的改动在于,不必区分前n天及之后的部分,可以使用一个while循环一并处理。
count = 0
n = len(apples)
current_day = 0# use a list to store all the information
# each element of the list has two components [i+days[i], apples[i]] # use heapq
import heapq
food = []
while food or current_day < n:'''codes''' current_day += 1
(1). 收获苹果:只在前n天访问apples和days数组
if current_day < n:# harverst applesif apples[current_day] != 0:heapq.heappush(food, [current_day + days[current_day], apples[current_day]])
(2). 吃苹果
if food:count += 1temp = heapq.heappop(food)if temp[1] > 1:# if we haven't eaten all the apples from food[0], then push it backheapq.heappush(food, [temp[0], temp[1] - 1])
(3). 丢弃将要腐烂的水果
# at the end of day, we need to throw away all rotten apples
while(len(food) > 0):if food[0][0] == current_day + 1:heapq.heappop(food)else:break
结果:通过全部测试用例,用时524ms,内存使用量19.3MB
完整代码:
class Solution:def eatenApples(self, apples: List[int], days: List[int]) -> int:count = 0n = len(apples)current_day = 0# use a list to store all the information# each element of the list has two components [i+days[i], apples[i]] # use heapqimport heapq food = []while food or current_day < n:# if we have apples or we can possibly harvest applesif current_day < n:# harverst applesif apples[current_day] != 0:heapq.heappush(food, [current_day + days[current_day], apples[current_day]])# then we need to find out whether we have an apple to eatif food:count += 1temp = heapq.heappop(food)if temp[1] > 1:# if we haven't eaten all the apples from food[0], then push it backheapq.heappush(food, [temp[0], temp[1] - 1]) # at the end of day, we need to throw away all rotten appleswhile(len(food) > 0):if food[0][0] == current_day + 1:heapq.heappop(food)else:breakcurrent_day += 1return count
4. 第三次优化
将food的每个元素从列表换成了元组,同时更改了一个判断语句。
更改:
1:添加元素
heapq.heappush(food, (current_day + days[current_day], apples[current_day]))
2. 将没吃完的food[0]推回去
d, nb = heapq.heappop(food)
if nb > 1:# if we haven't eaten all the apples from food[0], then push it backheapq.heappush(food, (d, nb - 1))
结果:通过测试用例。用时360ms,内存使用量19MB。
完整代码:
class Solution:def eatenApples(self, apples: List[int], days: List[int]) -> int:count = 0n = len(apples)current_day = 0# use a list to store all the information# each element of the list has two components [i+days[i], apples[i]] # use heapqimport heapq food = []while food or current_day < n:# if we have apples or we can possibly harvest applesif current_day < n and apples[current_day] != 0:# harverst apples heapq.heappush(food, (current_day + days[current_day], apples[current_day]))# then we need to find out whether we have an apple to eatif food:count += 1d, nb = heapq.heappop(food)if nb > 1:# if we haven't eaten all the apples from food[0], then push it backheapq.heappush(food, (d, nb - 1))# at the end of day, we need to throw away all rotten appleswhile food:if food[0][0] == current_day + 1:heapq.heappop(food)else:breakcurrent_day += 1return count
相关知识
1. 优先队列
这篇关于Leetcode 1705. 吃苹果的最大数目(medium)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!