Difficulty: Easy | Acceptance: 75.50% | Paid: No Topics: Math
You are given a positive integer arrivalTime denoting the arrival time of a train in hours, and an integer delayedTime denoting the amount of delay in hours.
Return the time when the train will arrive at the station.
The time is in 24-hour format (0 to 23).
- Examples
- Constraints
- Modulo Arithmetic
- Conditional Logic
Examples
Example 1
Input:
arrivalTime = 15, delayedTime = 5
Output:
20
Explanation: Arrival time of the train was 15:00 hours. It is delayed by 5 hours. Now it will reach at 15+5 = 20 (20:00 hours).
Example 2
Input:
arrivalTime = 13, delayedTime = 11
Output:
0
Explanation: Arrival time of the train was 13:00 hours. It is delayed by 11 hours. Now it will reach at 13+11=24 (Which is denoted by 00:00 in 24 hours format so return 0).
Constraints
- 1 <= arrivaltime < 24
- 1 <= delayedTime <= 24
Modulo Arithmetic
Intuition Since the time operates on a 24-hour cycle, we can use the modulo operator to find the remainder after dividing the total time by 24. This effectively handles the wrap-around from 23 to 0.
Steps
- Calculate the total time by adding arrivalTime and delayedTime.
- Return the total time modulo 24.
class Solution:
def findDelayedArrivalTime(self, arrivalTime: int, delayedTime: int) -> int:
return (arrivalTime + delayedTime) % 24Complexity
- Time: O(1)
- Space: O(1)
- Notes: This is the most optimal approach as it uses a single arithmetic operation.
Conditional Logic
Intuition We can manually check if the sum of the arrival time and the delay exceeds a full day (24 hours). If it does, we subtract 24 to get the correct time on the next day.
Steps
- Calculate the sum of arrivalTime and delayedTime.
- If the sum is less than 24, return the sum.
- Otherwise, return the sum minus 24.
class Solution:
def findDelayedArrivalTime(self, arrivalTime: int, delayedTime: int) -> int:
total = arrivalTime + delayedTime
return total if total < 24 else total - 24Complexity
- Time: O(1)
- Space: O(1)
- Notes: Slightly more verbose than the modulo approach but equally efficient for constant time operations.