Biggest Window Between Visits
Imagine you're tracking visitor arrivals at a popular location. You have a log of timestamps representing when visitors arrive. The goal is to find the largest time window (duration) between any two consecutive visits in the log. This is useful for understanding peak times, staffing needs, and overall visitor flow.
Problem Description
You are given a list of timestamps representing the arrival times of visitors. These timestamps are in ascending order (earliest to latest). Your task is to determine the maximum difference (in the same time units as the input timestamps) between any two consecutive timestamps in the list. Essentially, you need to find the longest gap between consecutive arrivals.
What needs to be achieved: Calculate the maximum time difference between consecutive timestamps in a sorted list.
Key requirements:
- The input list will always be sorted in ascending order.
- The input list will contain at least two timestamps.
- The timestamps can represent any time unit (seconds, minutes, days, etc.).
Expected behavior: The function should return a single number representing the largest time difference found between consecutive timestamps.
Edge cases to consider:
- A list with only two timestamps.
- A list with many timestamps, where the largest gap might be at the beginning or end.
- Timestamps representing zero or negative time differences (though the input is guaranteed to be sorted, consider how this might affect calculations).
Examples
Example 1:
Input: [1, 5, 10, 15, 20]
Output: 5
Explanation: The differences between consecutive timestamps are 4, 5, 5, 5. The largest difference is 5.
Example 2:
Input: [10, 20, 30, 40, 50, 60]
Output: 10
Explanation: All consecutive timestamps have a difference of 10. The largest difference is 10.
Example 3:
Input: [1, 2, 100, 101]
Output: 99
Explanation: The differences are 1, 98, 1. The largest difference is 98.
Constraints
- The input list will contain between 2 and 1000 timestamps (inclusive).
- Timestamps will be non-negative integers.
- The input list will be sorted in ascending order.
- The time complexity of your solution should be O(n), where n is the number of timestamps. Avoid nested loops.
Notes
Think about how you can iterate through the list only once to find the maximum difference. Consider using variables to keep track of the current difference and the maximum difference seen so far. The sorted nature of the input is crucial for an efficient solution. Don't recalculate differences unnecessarily.