-
Input:
- List of performance =>
performances
- List of performance =>
-
Output
- List of optimizer performance =>
schedules
- List of optimizer performance =>
Write a program for Sally that takes a list of Performance objects as input and produces Sally’s optimal schedule.
An optimal schedule meets the following criteria:
- Sally wants to see the best performance at any given time. This may mean cutting one event short to see a higher priority performance, then returning to the original later.
- You can assume Sally has a teleportation device and can travel between stages instantaneously!
- If two performances starting at the same time have the same priority, Sally is happy to go to either one.
- There may also be gaps where no performances are on.
- Using NodeJS & TypeScript to solve
- Model: content model to present for
performance
&schedule
- Services: content
OptimizerService
toprocess
input andprovide
output data - Controllers: using for calling services and provide endpoint. In case I using it's to make some unit test with
jest
- I'm using
playground
to test from bash file - Scripts: content input data, expected data, scripts to build, run & verify code.
-
Each
performances
havestart
&finish
time, then I want to create a flat array to contain them ex: [start-1
,finish-1
,start-2
,finish-2
] =>events
-
With this
events
sort by time ASC and make this array unique by time -
Loop through
events
findperformances
are playing on this timeline. Sortperformances
should be play first =>earlyStarts
than other by condition highest priority, if same priority compare to start time then choose the first one =>performanceEvents
-
By
performanceEvents
compare previous and current value- if
previous
priority smaller thancurrent
priority and is playing then setprevious
to stop and store this change inschedule
set finish time ofprevious
tocurrent
start time and return thisschedule
- if
previous
andcurrent
have same priority then continue playingprevious
, store thisprevious
inschedule
- if
previous
priority lager thancurrent
priority then prepare to playcurrent
, setcurrent
start time is the timeprevious
finish and store this change inschedule
and return thisschedule
Note: time store in
schedule
convert to ISO8601 date format, other case instead returnschedule
return null - if
-
Finally have an array of
schedule
then make sure this array is unique by comparing each properties
- using
yarn test
- using scripts
./scripts/verify.sh
- With
timezone.json
input data, can process and give correctschedule
but cannot return to the timezone of this file. - I think this conflict with requirement
DateTime’s are represented as strings in ISO8601 format.
=> input not in ISO8601 format. - In real case, it should convert by user side or should clarify input timezone and output timezone.