检查是否可以在 Python 中使用不同的注释为客户队列提供服务
假设我们有一个名为notes的数组,代表队列中客户持有的不同卢比纸币。他们都在等着买价值50卢比的门票。这里可能的注释是[50、100和200]。我们必须检查我们是否可以按顺序向人们出售门票,最初我们手上有0卢比。
因此,如果输入类似于notes=[50,50,100,100],那么前两个的输出将为True,我们不需要返回任何内容,但现在我们有两个50卢比的纸币。因此,对于最后两张,我们可以退还50卢比的钞票并按顺序出售所有门票。
示例
让我们看看以下实现以获得更好的理解-
from collections import defaultdict def solve(notes): freq = defaultdict(int) i = 0 while i < len(notes): if notes[i] == 50: freq[50] += 1 elif notes[i] == 100: freq[100] += 1 if freq[50] == 0: break freq[50] -= 1 else: if freq[100] > 0 and freq[50] > 0: freq[100] -= 1 freq[50] -= 1 elif freq[50] >= 3: freq[50] -= 3 else: break i += 1 if i == len(notes): return True return False notes = [50, 50, 100, 100] print(solve(notes))
输入
[50, 50, 100, 100]输出结果
True