习题16:卖票

Description:

The new "Avengers" movie has just been released! There are a lot of people at the cinema box office standing in a huge line. Each of them has a single 100, 50 or 25 dollars bill. A "Avengers" ticket costs 25 dollars.

Vasya is currently working as a clerk. He wants to sell a ticket to every single person in this line.

Can Vasya sell a ticket to each person and give the change if he initially has no money and sells the tickets strictly in the order people follow in the line?

Return YES, if Vasya can sell a ticket to each person and give the change. Otherwise return NO.

def tickets(people):
    #giving a change: change 25$ for 50$ and change 50$+25$ or 25$*3 for 100$
    change = {
        25: 0,
        50: 0,
    }
    for money in people:
        if money == 25:
            change[25] += 1
        elif money == 50:
            change[50] += 1
            change[25] -= 1
        else:
            if change[50] >= 1:
                change[50] -= 1
                change[25] -= 1
            else:
                change[25] -= 3
        if change[25] < 0:
            return 'NO'
    return 'YES'
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容