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 dollar bill. An "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 every person and give change if he initially has no money and sells the tickets strictly in the order people queue?
Return YES, if Vasya can sell a ticket to every person and give change with the bills he has at hand at that moment. Otherwise return NO.
# 翻譯:某家電影院,賣一張門票是 25 元,只接受客戶給 25 元、50 元、100 元三種面額,假設從開店就沒有任何錢的情況下,如果店員能在服務完所有客人後中間都順利找錢就回傳 'YES' 否則 'NO'
def tickets(people)
end
RSpec.describe "Vasya - Clerk" do
it "ticket" do
expect(tickets([25, 25, 50])).to eq('YES')
expect(tickets([25, 100])).to eq('NO')
expect(tickets([50, 100, 100])).to eq('NO')
end
end
def tickets(people)
counter_25, counter_50 = 0, 0
people.each do |pay|
counter_25 += 1 if pay == 25
if pay == 50
counter_25 -= 1
counter_50 += 1
end
if pay == 100
if counter_25 >= 3
counter_25 -= 3
else
counter_25 -= 1
counter_50 -= 1
end
end
end
if counter_25 < 0 || counter_50 < 0
'NO'
else
'YES'
end
end