, , , , . , .
Ruby, .
, , 2-4, - :
[ 1 , 1 , 1 ],
[ 1 , 1 , 1 ],
[ 1 , 1 , 1 ],
. .
calendar = [ # 0 is free, 1 is busy
[ 1 , 1 , 1 ], #12AM to
[ 1 , 1 , 1 ], #1AM to
[ 1 , 1 , 1 ], #2AM to
[ 1 , 1 , 1 ], #3AM to
[ 1 , 1 , 1 ], #4AM to
[ 1 , 1 , 0 ], #5AM to
[ 1 , 1 , 0 ], #6AM to
[ 1 , 1 , 0 ], #7AM to
[ 1 , 1 , 0 ], #8AM to
[ 0 , 1 , 1 ], #9AM to
[ 0 , 1 , 1 ], #10AM to
[ 1 , 1 , 1 ], #11AM to
[ 1 , 1 , 1 ], #12PM to
[ 1 , 0 , 1 ], #1PM to
[ 1 , 0 , 1 ], #2PM to
[ 1 , 0 , 1 ], #3PM to
[ 1 , 1 , 0 ], #4PM to
[ 1 , 1 , 0 ], #5PM to
[ 1 , 1 , 1 ], #6PM to
[ 1 , 1 , 1 ], #7PM to
[ 1 , 1 , 1 ], #8PM to
[ 1 , 1 , 1 ], #9PM to
[ 1 , 1 , 1 ], #10PM to
[ 1 , 1 , 1 ], #11PM to
["A","B","C"] #Users
]
def find_available_slot(length, calendar)
[].tap do |results|
calendar.transpose.collect do |schedule|
times = schedule[0...-1]
blocks = sort_it(times.each_index.select {|i| times[i] == 0 }).select { |x| x.count >= length }
results << [blocks, schedule.last] unless blocks.empty?
end
results
end
end
def sort_it(arr)
tmp, main = [], []
arr.each_with_index do |x, i|
if arr[i-1]
if arr[i-1] + 1 == x
tmp << x
else
main << tmp unless tmp.empty?
tmp = [x]
end
else
tmp << x
end
end
main << tmp
main
end
find_available_slot(2, calendar)
, 2 , :
=> [[[[9, 10]], "A"], [[[13, 14, 15]], "B"], [[[5, 6, 7, 8], [16, 17]], "C"]]
, , , . , [0] [0] [0] , [0] [1] , .
, , 2d, .
Google, :
(N N , )
FInd O (n) ?
http://www.geeksforgeeks.org/given-n-appointments-find-conflicting-appointments/