Balloon launch window
MediumOnline Assessment
Schedule the launches of a hot-air balloon festival, whose single launch pad has to wait for the wind.
The wind forecast wind gives the wind speed in knots for every minute of the morning: wind[m] is the speed during minute m, and the field closes after minute len(wind) - 1. Each balloon is [name, ready, maxWind]: its crew has it inflated and ready at minute ready, and it can only be flown in wind of at most maxWind knots. Minutes are processed in order, m = 0, 1, 2, .... In each minute at most one balloon launches, and only if all of this holds for it:
- Pad free: no balloon has launched in the previous
cooldown - 1minutes. In other words, after a launch at minuteL, the next launch may be at minuteL + cooldownat the earliest. - Ready, and still waiting:
ready <= m <= ready + patience. A crew that has waited more thanpatienceminutes packs up, and its balloon never launches. - Safe climb-out: the wind in the launch minute and in the following two minutes,
wind[m],wind[m+1]andwind[m+2], is all at mostmaxWind. Minutes beyond the end of the forecast are not checked.
When more than one balloon qualifies in a minute, the pad takes the balloon with the lowest maxWind, because it is the one that may not get another calm moment. Ties go to the balloon with the earlier ready, and then to the one listed first.
Input: wind, a list of integers; balloons, a list of [name, ready, maxWind]; cooldown and patience, integers.
Output: a list with one integer per balloon, in the order of balloons: the minute it launched, or -1 if it never did.
Example 1
- Input:
- [[6,6,9,12,6,5,5,7,10,4,4,4],[["red",0,10],["blue",2,7],["gold",3,12]],3,4]
- Output:
- [0,-1,3]
- Explanation
redis ready and calm enough at minute 0 (winds 6, 6, 9 are all at most 10), so it launches. The pad is busy until minute 3. At minute 3,blue(limit 7) sees a wind of 12 and cannot go, butgold(limit 12) can.bluethen cannot launch at minute 6 either (the wind reaches 10 two minutes later) and its crew leaves after minute 6 (2 + 4), so it never launches.
Example 2
- Input:
- [[4,4,4,4,9,4,4,4,4,4],[["a",0,8],["b",0,5],["c",0,5],["d",1,6]],2,6]
- Output:
- [-1,0,5,7]
- Explanation
- At minute 0,
a,bandcall qualify;bandchave the lowest limit (5) and the same ready minute, sob, listed first, launches. The wind of 9 at minute 4 blocks every launch in minutes 2 to 4. At minute 5,a,canddqualify;chas the lowest limit and launches. The pad is busy until minute 7, whenahas run out of patience (0 + 6 = 6) anddlaunches.
Example 3
- Input:
- [[3,3,3,3,3,3,8],[["x",4,5],["y",5,4],["z",6,9]],2,1]
- Output:
- [-1,-1,6]
- Explanation
- A launch at minute 4 or 5 would be hit by the wind of 8 at minute 6, so
xandy(limits 5 and 4) cannot go, even though the wind is calm at the launch minute itself. The forecast ends after minute 6, so near the end fewer than three minutes are checked. At minute 6,xis out of patience,yis blocked by the wind of 8, andz(limit 9) launches.
Constraints
0 <= wind.length <= 1,500;0 <= wind[m] <= 400 <= balloons.length <= 80;readyis between0and1,500,1 <= maxWind <= 40; names are unique1 <= cooldown <= 30;0 <= patience <= 1,500