File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ function solution ( book_time ) {
2+ const toMinutes = ( time ) => {
3+ const [ hour , minute ] = time . split ( ':' ) . map ( Number ) ;
4+ return hour * 60 + minute ;
5+ } ;
6+
7+ book_time . sort ( ( a , b ) => toMinutes ( a [ 0 ] ) - toMinutes ( b [ 0 ] ) ) ;
8+
9+ const rooms = [ ] ;
10+
11+ for ( const [ start , end ] of book_time ) {
12+ const startTime = toMinutes ( start ) ;
13+ const endTime = toMinutes ( end ) + 10 ;
14+
15+ let earliestRoomIndex = rooms . findIndex (
16+ ( roomEndTime ) => roomEndTime <= startTime
17+ ) ;
18+
19+ if ( earliestRoomIndex !== - 1 ) {
20+ rooms [ earliestRoomIndex ] = endTime ;
21+ } else {
22+ rooms . push ( endTime ) ;
23+ }
24+
25+ rooms . sort ( ( a , b ) => a - b ) ;
26+ }
27+
28+ return rooms . length ;
29+ }
30+
31+ console . log (
32+ solution ( [
33+ [ '15:00' , '17:00' ] ,
34+ [ '16:40' , '18:20' ] ,
35+ [ '14:20' , '15:20' ] ,
36+ [ '14:10' , '19:20' ] ,
37+ [ '18:20' , '21:20' ] ,
38+ ] )
39+ ) ;
You can’t perform that action at this time.
0 commit comments