Calculate Delayed Arrival Time

We can find the delayed arrival time by adding the arrival time and the delayed time together. Since the time is in 24-hour format, if the result exceeds or equals 24, we need to subtract 24 from it to get the equivalent time on the next day.

Here’s a step-by-step explanation:

  1. Add Arrival and Delayed Time: Sum the arrivalTime and delayedTime.
  2. Adjust for 24-hour Format: If the sum is 24 or more, subtract 24 to convert to 24-hour format.

Python solution:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
class Solution:
    def findDelayedArrivalTime(self, arrivalTime: int, delayedTime: int) -> int:
        # Add the arrival time and the delayed time
        total_time = arrivalTime + delayedTime

        # Adjust for 24-hour format if total time is 24 or more
        if total_time >= 24:
            total_time -= 24

        return total_time

Explanation:

  • If the arrival time is 15 and the delay is 5, then 15 + 5 = 20, so the train arrives at 20:00.
  • If the arrival time is 13 and the delay is 11, then 13 + 11 = 24, but in 24-hour format, we represent this as 00:00, so the function returns 0.

The code handles both cases and returns the correct arrival time in 24-hour format. Its time complexity is O(1).