Rust solution. This is easy when you use the right data structure - in this case, a heap.
usestd::collections::BinaryHeap;usestd::cmp::Reverse;fnqueue_time(customers:&[usize],n:usize)->usize{assert!(0<n,"can't serve anyone without registers");letmutcustomers=customers.iter().copied().fuse();// The first n customers start at 0 and finish at their serving time.letmutfinish_times:BinaryHeap<_>=customers.by_ref().take(n).map(Reverse).collect();forserving_timeincustomers{// `finish_times.pop()` returns the next time a customer finishes.letReverse(current_time)=finish_times.pop().expect("finish_times should not be empty here - we add a new entry for each one we take");// This register was cleared at `current_time`, and will be cleared again after// serving_time passes.finish_times.push(Reverse(current_time+serving_time));}// No more pending customers - so just wait for the last one in the registers.finish_times.drain().map(|Reverse(time)|time).max()// `finish_times` can only be empty if customers was empty to begin with..unwrap_or(0)}fnmain(){assert_eq!(queue_time(&[5,3,4],1),12);assert_eq!(queue_time(&[10,2,3,3],2),10);assert_eq!(queue_time(&[2,3,10],2),12);}
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
Rust solution. This is easy when you use the right data structure - in this case, a heap.