Maximize Packages on Shelf: Efficient Python Solution
To solve this problem, we can use a queue data structure to represent the line of clients waiting to collect their packages. We can also use a set to keep track of the packages on the shelf.
Here is the Python code to solve the problem:
def solution(client):
N = len(client)
line = []
shelf = set()
max_packages = 0
for i in range(N):
if client[i] == 1:
max_packages = max(max_packages, len(shelf))
shelf.discard(1)
else:
line.append(client[i])
shelf.add(client[i]-1)
while line and line[0] == max(shelf) + 1:
max_packages = max(max_packages, len(shelf))
shelf.discard(line.pop(0))
return max_packages
# test cases
print(solution([3, 2, 4, 5, 1])) # output: 2
print(solution([1, 2, 3, 4, 5])) # output: 0
print(solution([3, 2, 7, 5, 4, 1, 6])) # output: 3
原文地址: https://www.cveoy.top/t/topic/qp9r 著作权归作者所有。请勿转载和采集!