← All problems

3633. Earliest Finish Time for Land and Water Rides I

EasyOpen on LeetCodeProblem statement

Problem Statement

3633. Earliest Finish Time for Land and Water Rides I

Easy


You are given two categories of theme park attractions: land rides and water rides.

A tourist must experience exactly one ride from each category, in either order.

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 gives the earliest finish time of 9.

Example 2:

Input: landStartTime = [5], landDuration = [3], waterStartTime = [1], waterDuration = [10]

Output: 14

Explanation:​​​​​​​

Plan A provides the earliest finish time of 14.​​​​​​​

 

Constraints:

Java

Source file
class Solution {
    public int earliestFinishTime(int[] landStartTime, int[] landDuration, int[] waterStartTime, int[] waterDuration) {
        int ln = landStartTime.length, wn = waterStartTime.length;
        int t1 = Integer.MAX_VALUE, t2 = Integer.MAX_VALUE;
        for (int i = 0; i < ln; i++) // min time to end land ride
            t1 = Math.min(t1, landStartTime[i] + landDuration[i]);
        for (int i = 0; i < wn; i++) // min time to end water ride
            t2 = Math.min(t2, waterStartTime[i] + waterDuration[i]);
        int T1 = Integer.MAX_VALUE, T2 = Integer.MAX_VALUE;
        for (int i = 0; i < wn; i++) // min time to end water ride after land
            T1 = Math.min(T1, Math.max(t1, waterStartTime[i]) + waterDuration[i]);
        for (int i = 0; i < ln; i++) // min time to end land ride after water
            T2 = Math.min(T2, Math.max(t2, landStartTime[i]) + landDuration[i]);
        return Math.min(T1, T2);
    }
}