【codewar-6kyu】Vasya - Clerk

##題目描述

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 single10050 or 25 dollars bill. A "Avengers" ticket costs 25 dollars.this

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?spa

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

Examples:

1 ### Python ###
2 
3 tickets([25, 25, 50]) # => YES 
4 tickets([25, 100]) # => NO. 

## 思路分析

在這倒編碼毫無難度純看邏輯的題上,竟然卡了半天,整我的都很差了,沒好好讀題沒好好思考,讓我去死一死blog

賣25塊一張的票,可能會收到2五、50、100的錢;ci

售票員不帶零錢,面對什麼樣的隊伍才能找得開零錢,全賣得了票呢?it

要按順序賣票!!class

收的錢就留下了!!sed

25開心手下;50必須找25;100優先找50,沒有就找3張25(固然優先找50啦!50的只能找給100的,25還得儘可能留着給50的呢!在這兒卡了是否是傻……)di

## 代碼解析

Python:

 1 def tickets(people):
 2     n25 = n50 = n100 = 0
 3     for e in people:
 4         if   e==25            : n25+=1
 5         elif e==50            : n25-=1; n50+=1
 6         elif e==100 and n50>0 : n25-=1; n50-=1
 7         elif e==100 and n50==0: n25-=3
 8         if n25<0 or n50<0:
 9             return 'NO'
10     return 'YES'

就是簡簡單單按照收錢找錢的過程來就行了,少年不要想太多。

相關文章
相關標籤/搜索