Description
You are given two categories of theme park attractions: land rides and water rides.
- Land rides
landStartTime[i]– the earliest time theithland ride can be boarded.landDuration[i]– how long theithland ride lasts.
- Water rides
waterStartTime[j]– the earliest time thejthwater ride can be boarded.waterDuration[j]– how long thejthwater ride lasts.
A tourist must experience exactly one ride from each category, in either order.
- A ride may be started at its opening time or any later moment.
- If a ride is started at time
t, it finishes at timet + duration. - Immediately after finishing one ride the tourist may board the other (if it is already open) or wait until it opens.
Return the earliest possible time at which the tourist can finish both rides.
Example 1:
Input: landStartTime = [2,8], landDuration = [4,1], waterStartTime = [6], waterDuration = [3]
Output: 9
Explanation:
- Plan A (land ride 0 → water ride 0):
- Start land ride 0 at time
landStartTime[0] = 2. Finish at2 + landDuration[0] = 6. - Water ride 0 opens at time
waterStartTime[0] = 6. Start immediately at6, finish at6 + waterDuration[0] = 9.
- Start land ride 0 at time
- Plan B (water ride 0 → land ride 1):
- Start water ride 0 at time
waterStartTime[0] = 6. Finish at6 + waterDuration[0] = 9. - Land ride 1 opens at
landStartTime[1] = 8. Start at time9, finish at9 + landDuration[1] = 10.
- Start water ride 0 at time
- Plan C (land ride 1 → water ride 0):
- Start land ride 1 at time
landStartTime[1] = 8. Finish at8 + landDuration[1] = 9. - Water ride 0 opened at
waterStartTime[0] = 6. Start at time9, finish at9 + waterDuration[0] = 12.
- Start land ride 1 at time
- Plan D (water ride 0 → land ride 0):
- Start water ride 0 at time
waterStartTime[0] = 6. Finish at6 + waterDuration[0] = 9. - Land ride 0 opened at
landStartTime[0] = 2. Start at time9, finish at9 + landDuration[0] = 13.
- Start water ride 0 at time
Plan A gives the earliest finish time of 9.
Example 2:
Input: landStartTime = [5], landDuration = [3], waterStartTime = [1], waterDuration = [10]
Output: 14
Explanation:
- Plan A (water ride 0 → land ride 0):
- Start water ride 0 at time
waterStartTime[0] = 1. Finish at1 + waterDuration[0] = 11. - Land ride 0 opened at
landStartTime[0] = 5. Start immediately at11and finish at11 + landDuration[0] = 14.
- Start water ride 0 at time
- Plan B (land ride 0 → water ride 0):
- Start land ride 0 at time
landStartTime[0] = 5. Finish at5 + landDuration[0] = 8. - Water ride 0 opened at
waterStartTime[0] = 1. Start immediately at8and finish at8 + waterDuration[0] = 18.
- Start land ride 0 at time
Plan A provides the earliest finish time of 14.
Constraints:
1 <= n, m <= 100landStartTime.length == landDuration.length == nwaterStartTime.length == waterDuration.length == m1 <= landStartTime[i], landDuration[i], waterStartTime[j], waterDuration[j] <= 1000
Solutions
This code calculates the earliest possible time to complete a two-step sequence consisting of one land task and one water task, where one task must finish before the other can begin. To find the fastest schedule, the code first calculates the absolute earliest time any land task can finish, and then determines how quickly each water task could finish if it had to wait for that land task to complete (or start at its own scheduled time if that is later). It then performs the reverse scenario: it finds the earliest finishing water task and calculates the completion times for all land tasks waiting on it. Finally, it compares all of these combinations and returns the absolute minimum completion time possible.
/**
* @param {number[]} landStartTime
* @param {number[]} landDuration
* @param {number[]} waterStartTime
* @param {number[]} waterDuration
* @return {number}
*/
var earliestFinishTime = function(landStartTime, landDuration, waterStartTime, waterDuration) {
let m = landStartTime.length;
let n = waterStartTime.length;
let res = Infinity;
let minl = Infinity;
for (let i = 0; i < m; i++) {
minl = Math.min(minl, landStartTime[i] + landDuration[i]);
}
let minw = Infinity;
for (let i = 0; i < n; i++) {
minw = Math.min(minw, waterStartTime[i] + waterDuration[i]);
res = Math.min(res, Math.max(minl, waterStartTime[i]) + waterDuration[i]);
}
for (let i = 0; i < m; i++) {
res = Math.min(res, Math.max(minw, landStartTime[i]) + landDuration[i]);
}
return res;
};