Last active
June 1, 2016 17:43
-
-
Save bipinshashi/9e5d476197bd514c14c5fcfb3a7af2c4 to your computer and use it in GitHub Desktop.
CF_teeshirt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
=begin | |
CodeFights expands their t-shirt selection so that there is one color corresponding to each level (Gray/Recruit, Green/Trainee, Blue/Soldier, Yellow/Warrior, Red/Captain, Black/Ninja). | |
At the beginning of each month CodeFights receives a shipment of n t-shirts. There will always be an equal number of each color. Each user can only choose between 2 different colors when ordering a t-shirt. At the end of each month CodeFights takes all received orders and sends t-shirts, but only if it is possible to grant all requests. This explains why the process sometimes takes longer than expected (just teasing ^_^). | |
Given n and the list of orders placed, determine if CodeFights has enough t-shirts to fulfill all orders (true) or must wait until the next month (false). | |
Example | |
For n = 6 and orders = [["Red", "Black"],["Red", "Black"]], | |
the output should be | |
codefightsTshirts(n, orders) = true. | |
CodeFights has 6 shirts in stock, which means 1 of each color. If they send a red t-shirt for the first order, we can still send a black t-shirt for the second order. Thus, both orders can be fulfilled. | |
For n = 6 and | |
orders = [["Red", "Black"], ["Red", "Black"], ["Red", "Black"]] | |
the output should be | |
codefightsTshirts(n, orders) = false. | |
Again, there is 1 t-shirt of each color. It is possible to fulfill the first two orders by sending 1 red and 1 black shirt, however there won't be any red/black shirts to fulfill the third order. Thus, it's impossible to fulfill all orders this month. | |
For n = 6 and | |
orders = [["Red", "Black"], ["Blue", "Black"], ["Red", "Blue"]] | |
the output should be | |
codefightsTshirts(n, orders) = true. | |
first order is black, second order blue and third order is red | |
=end | |
def codefightsTshirts(n, orders) | |
tees,count= n/6, {} | |
backup = Hash.new([]) | |
["Gray", "Green", "Blue", "Yellow", "Red", "Black"].map {|c| count[c] = tees } | |
orders.each do |order| | |
if count[order[0]] > 0 | |
count[order[0]] -= 1 | |
backup[order[0]] << order[1] | |
elsif count[order[1]] > 0 | |
count[order[1]] -= 1 | |
else | |
found = false | |
while backup[order[0]].count > 0 | |
color = backup[order[0]].pop | |
if count[color] > 0 | |
count[color] -= 1 | |
found = true | |
end | |
end | |
while found == false && backup[order[1]].count > 0 | |
color = backup[order[1]].pop | |
if count[color] > 0 | |
count[color] -= 1 | |
found = true | |
end | |
end | |
return false if found == false | |
end | |
end | |
return true | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
damn, ok my approach is wrong, figured out the hidden test case that was failing - added to the last of the comments - so apparently the order within the array does not matter