Leetcode 2651. Calculate Delayed Arrival Time
You are given a scheduled arrival time of a train in 24-hour format (0-23). The train can be delayed by a certain amount of hours. Write a function that returns the delayed arrival time in the same 24-hour format.
Example:
arrivalTime = 22, delayedTime = 5
3
Constraints:
0 <= arrivalTime < 24
1 <= delayedTime <= 24
To solve this problem, we need to calculate the new arrival time after the delay in a 24-hour format. We can achieve this by adding the delayed hours to the scheduled arrival time and taking the result modulo 24. This handles the wrap-around for times greater than or equal to 24 hours.
Steps:
arrivalTime
and delayedTime
.The time complexity is O(1), as the operations required (addition and modulo) take constant time.
Here is the implementation in C++:
#include <iostream>
int findDelayedArrivalTime(int arrivalTime, int delayedTime) {
return (arrivalTime + delayedTime) % 24;
}
int main() {
int arrivalTime = 22;
int delayedTime = 5;
std::cout << "Delayed arrival time: " << findDelayedArrivalTime(arrivalTime, delayedTime) << std::endl;
return 0;
}
This code defines the function findDelayedArrivalTime
that computes the new arrival time considering the delay and handles the 24-hour wrap-around using the modulo operator. The main
function demonstrates how to use this function with an example input and prints the result.
Got blindsided by a question you didn’t expect?
Spend too much time studying?
Or simply don’t have the time to go over all 3000 questions?