LeetCode 2651 - Calculate Delayed Arrival Time

The problem asks us to calculate the arrival time of a train given its scheduled arrivalTime and the amount of delayedTime. Both times are expressed in hours using the 24-hour clock format. The goal is to determine the new arrival time after accounting for the delay.

LeetCode Problem 2651

Difficulty: 🟢 Easy
Topics: Math

Solution

Problem Understanding

The problem asks us to calculate the arrival time of a train given its scheduled arrivalTime and the amount of delayedTime. Both times are expressed in hours using the 24-hour clock format. The goal is to determine the new arrival time after accounting for the delay. For example, if a train was scheduled to arrive at 15:00 and is delayed by 5 hours, the new arrival time is 20:00. Importantly, if the resulting time reaches or exceeds 24, it wraps around, so 24 becomes 0, 25 becomes 1, and so on.

The inputs are guaranteed to be positive integers with 1 <= arrivalTime < 24 and 1 <= delayedTime <= 24. This ensures that we will not have negative numbers or invalid 24-hour times. The key edge cases to consider include delays that push the arrival time exactly to 24 hours or beyond, which requires wrapping around to the start of the day.

The problem is straightforward mathematically, but attention must be paid to modular arithmetic because the 24-hour format wraps around.

Approaches

A naive or brute-force approach would involve manually incrementing the arrival time one hour at a time until the delay is fully applied. This would be correct because it simulates the passage of each delayed hour, but it is unnecessarily slow and cumbersome for larger delays. For the given constraints, this approach would work, but it is inefficient and overly verbose.

The optimal approach relies on the mathematical property of modular arithmetic. Since the 24-hour clock wraps every 24 hours, we can directly compute the delayed arrival time using:

newArrivalTime = (arrivalTime + delayedTime) % 24

This ensures that the time is automatically wrapped around if it reaches or exceeds 24, giving the correct 24-hour format directly. This approach is both simple and highly efficient.

Approach Time Complexity Space Complexity Notes
Brute Force O(delayedTime) O(1) Increment arrival time hour by hour until delay is exhausted
Optimal O(1) O(1) Use modular arithmetic to compute the result directly

Algorithm Walkthrough

  1. Take the input arrivalTime and delayedTime.
  2. Add the delayedTime to arrivalTime to compute the total hours.
  3. Apply modulo 24 on the total hours to ensure the time wraps correctly in the 24-hour format.
  4. Return the result as the new arrival time.

Why it works: The modulo operation guarantees that any time beyond 23 hours wraps around to the correct hour on a 24-hour clock. For example, (13 + 11) % 24 = 24 % 24 = 0. This correctly handles the edge case where the delayed time causes a wrap-around to midnight.

Python Solution

class Solution:
    def findDelayedArrivalTime(self, arrivalTime: int, delayedTime: int) -> int:
        # Add the delay to the arrival time
        total_time = arrivalTime + delayedTime
        # Wrap around 24-hour format using modulo
        delayed_arrival = total_time % 24
        return delayed_arrival

The Python implementation directly follows the algorithm. We first compute total_time by adding the delay to the original arrival. Using the modulo operator, we wrap the time around 24 hours. This single line captures the entire logic efficiently.

Go Solution

func findDelayedArrivalTime(arrivalTime int, delayedTime int) int {
    // Add delay to arrival time
    totalTime := arrivalTime + delayedTime
    // Wrap around 24-hour format using modulo
    return totalTime % 24
}

In Go, the solution is conceptually identical to Python. We use integer addition and modulo to compute the delayed arrival time. There is no need for extra type handling since Go's integers naturally support this arithmetic.

Worked Examples

Example 1: arrivalTime = 15, delayedTime = 5

Step total_time total_time % 24 delayed_arrival
Compute total_time 15 + 5 = 20 20 % 24 = 20 20

Output: 20

Example 2: arrivalTime = 13, delayedTime = 11

Step total_time total_time % 24 delayed_arrival
Compute total_time 13 + 11 = 24 24 % 24 = 0 0

Output: 0

Complexity Analysis

Measure Complexity Explanation
Time O(1) Only addition and modulo operations are performed, both constant time
Space O(1) Only a few integer variables are used, no extra data structures

The complexity is minimal because the solution requires only a constant number of operations regardless of input values. The space usage is negligible since no arrays or other structures are allocated.

Test Cases

# Provided examples
assert Solution().findDelayedArrivalTime(15, 5) == 20  # simple addition
assert Solution().findDelayedArrivalTime(13, 11) == 0  # wrap around to 0

# Boundary values
assert Solution().findDelayedArrivalTime(1, 1) == 2  # minimal values
assert Solution().findDelayedArrivalTime(23, 1) == 0  # wrap around midnight
assert Solution().findDelayedArrivalTime(23, 24) == 23  # full day delay

# Additional cases
assert Solution().findDelayedArrivalTime(5, 24) == 5  # delay equal to full day
assert Solution().findDelayedArrivalTime(12, 12) == 0  # exactly half-day delay
Test Why
15,5 basic addition without wrap
13,11 addition results in exactly 24, wraps to 0
1,1 smallest input values
23,1 wraps past midnight
23,24 full-day delay, should return original time
5,24 full-day delay, confirms modulo correctness
12,12 half-day delay, wraps to 0

Edge Cases

One important edge case occurs when the total time equals exactly 24. Without modulo, a naive addition would return 24, which is invalid in 24-hour format. Our solution correctly wraps it to 0.

Another edge case is when the delayedTime is 24, which is allowed by constraints. This means the train is delayed by exactly one full day, so the arrival time remains unchanged. The modulo operation naturally handles this.

Finally, when the arrivalTime is near the end of the day, such as 23, and the delay pushes it past midnight, a naive solution without modulo would fail. Using (arrivalTime + delayedTime) % 24 ensures it wraps correctly and returns a valid 24-hour time.