Problem 271100 · medium · Level 02 Linear Data Structures

Longest Run of Ticket Numbers

sets · sequences · amortised analysis

Support tickets have integer ids in ids (unordered, with possible duplicates). A run is a set of consecutive integers x, x+1, ..., x+L-1 all present in ids. Return the length of the longest run, or 0 for an empty list.

Examples

Input:  ids = [41, 12, 15, 900, 14, 13, 40, 11]
Output: 5
Explanation: 11, 12, 13, 14, 15 (40, 41 is a shorter run).

Input:  ids = [7, 7, 7]
Output: 1

Constraints

  • 0 <= len(ids) <= 10**5
  • -10**9 <= ids[i] <= 10**9
  • Target complexity: O(n) time. Sorting is O(n log n); aim for the set-based approach.

Goals

  • Use a set to test neighbours in O(1)
  • Start counting only from the beginning of a run
Starting Python…