Show problems as
Classic problem #3633EasyArrayTwo PointersBinary SearchGreedySorting

Earliest Finish Time for Land and Water Rides I

Real-world version: Forge and Kiln Booking
Tracked in this browser only
Read Earliest Finish Time for Land and Water Rides I on LeetCode(opens in a new tab)

The statement here is our own: the same inputs, outputs and technique.

An apprentice has to be signed off on exactly one forge session and exactly one kiln session before the day is over, and the two sessions may be taken in either order.

Forge session i unlocks at minute forgeOpen[i] and, once the apprentice begins it, occupies forgeLength[i] minutes without a break. Kiln session j unlocks at minute kilnOpen[j] and occupies kilnLength[j] minutes. A session may be begun at any minute at or after the minute it unlocks, and the apprentice may idle for as long as needed while waiting.

The apprentice is free from minute 0, cannot be in both workshops at the same time, and must finish one session before beginning the other. Return the earliest minute at which both chosen sessions can be finished.

Examples

Example 1

Input
forgeOpen = [8, 3], forgeLength = [5, 6], kilnOpen = [1, 7], kilnLength = [4, 2]
Output
11

Forge session 1 unlocks at minute 3 and runs to minute 9. Kiln session 1 unlocks at minute 7, so it can begin at minute 9 and it runs to minute 11.

Example 2

Input
forgeOpen = [1], forgeLength = [1000], kilnOpen = [1000], kilnLength = [1]
Output
1002

The kiln session unlocks at minute 1000 and takes a single minute, ending at minute 1001. The forge session has been unlocked since minute 1, so it begins at minute 1001 and runs 1000 minutes to minute 2001.

Example 3

Input
forgeOpen = [9, 1], forgeLength = [1, 30], kilnOpen = [4, 40], kilnLength = [1, 1]
Output
10

Kiln session 0 unlocks at minute 4 and takes 1 minute, ending at minute 5. Forge session 0 unlocks at minute 9, so it waits until minute 9 and takes 1 minute, ending at minute 10.

Constraints

  • 1 <= forgeOpen.length == forgeLength.length <= 100
  • 1 <= kilnOpen.length == kilnLength.length <= 100
  • 1 <= forgeOpen[i], forgeLength[i], kilnOpen[j], kilnLength[j] <= 1000

The signature

The editor starts with this. It matches the parent problem's shape, so a solution that works here works on a standard judge unchanged.

Python
def earliest_both_done(forgeOpen: list[int], forgeLength: list[int], kilnOpen: list[int], kilnLength: list[int]) -> int:
Java
public int earliestBothDone(int[] forgeOpen, int[] forgeLength, int[] kilnOpen, int[] kilnLength)