Time-series data often arrives at nearly regular intervals. Delta-of-delta encoding stores how those intervals change, turning a steady stream of timestamps into a run of zeros that compresses efficiently.
Design a DeltaOfDeltaEncoder class:
DeltaOfDeltaEncoder() creates a stateless encoder.int[] encode(int[] timestamps) returns the delta-of-delta representation.
The output follows these rules:
- Keep the first timestamp unchanged.
- Store the first gap,
timestamps[1] - timestamps[0], as the second value when it exists. - For each later timestamp, store
currentGap - previousGap.
Example 1:
Input:
Output:
Explanation: Every gap is 60, so every change after the first gap is 0.
Example 2:
Input:
Output:
Explanation: The gaps are 60 and 70, so the final encoded value is 70 - 60 = 10.
Constraints
1 <= timestamps.length <= 10^5-10^9 <= timestamps[i] <= 10^9timestamps is non-decreasing.- Every computed difference fits in a signed 32-bit integer.
- At most
100 calls are made to encode.