#2651
Calculate Delayed Arrival Time
EasyMathMathModulo Arithmetic
Approaches
Brute ForceOptimal
Complexity Comparison
| Brute Force | Optimal Solution★ | |
|---|---|---|
| Time | O(1) | O(1) |
| Space | O(1) | O(1) |
💡
Intuition
Time O(1)Space O(1)
The optimal solution leverages the modulo operator to handle the wrap-around effect of the 24-hour format directly. This approach is efficient and avoids unnecessary checks.
⚙️
Algorithm
2 steps- 1Step 1: Calculate the new arrival time using the formula (arrivalTime + delayedTime) % 24.
- 2Step 2: Return the result directly.
solution.py5 lines
1def calculateDelayedArrivalTime(arrivalTime, delayedTime):
2 return (arrivalTime + delayedTime) % 24
3
4# Example usage
5print(calculateDelayedArrivalTime(15, 5))ℹ
Complexity note: This complexity remains constant as we are performing a fixed number of operations regardless of the input size.
- 1Using modulo helps to directly handle the wrap-around in 24-hour format.
- 2Both approaches yield the same result, but the optimal one is more efficient.
Solutions and explanations are original Tejav content. Problem titles © LeetCode — use the LeetCode button above for the full problem statement.