Created
July 6, 2017 02:51
-
-
Save wowdyaln/89476eba9eef49e47779e98fba118307 to your computer and use it in GitHub Desktop.
[codewar] The Supermarket Queue
This file contains 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
def queue_time(queue, cashier) | |
# 每個位置一起減去當中最小的值 | |
def subtract_the_min(arr) | |
arr.map do |ele| | |
ele - arr.min | |
end | |
end | |
# 如果 at_a_certain_moment 裡面有0 -> 找到這個位置,塞入下一個顧客 ->直到每個位置都不是0 | |
# 然後 subtract_the_min(at_a_certain_moment) | |
def be_filled(aacm, q) | |
while aacm.include?(0) do | |
if q === [] # 已經沒有顧客了,但還有閒置的cashier | |
return aacm | |
else | |
zero = aacm.find_index(0) | |
aacm[zero] = q.shift | |
end | |
end | |
aacm | |
end | |
if queue === [] | |
return 0 | |
else | |
at_a_certain_moment = queue.shift(cashier) | |
total_time = 0 | |
until queue === [] do | |
total_time += at_a_certain_moment.min | |
at_a_certain_moment = subtract_the_min(at_a_certain_moment) | |
at_a_certain_moment = be_filled(at_a_certain_moment, queue) | |
end | |
# 當 queue 被拿完的時候,total_time 加上 at_a_certain_moment 裡面的最大值 | |
total_time += at_a_certain_moment.max | |
total_time | |
end | |
end | |
##### 神人答案,太神了,逆向思考用疊加上去!!! ##### | |
cL0wnb0at | |
def queue_time(customers, n) | |
tills = [0] * n | |
while customers.length > 0 | |
minIndex = tills.index(tills.min) | |
tills[minIndex] += customers.shift #美妙的寫法! | |
end | |
tills.max | |
end | |
##### 神人答案2##### | |
matthewrr | |
def queue_time(customers, n) | |
arr = Array.new(n, 0) | |
customers.each { |customer| arr[arr.index(arr.min)] += customer } | |
arr.max | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment