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 = [100, 4, 200, 1, 3, 2]
Output: 4
Explanation: 1, 2, 3, 4.

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…