FastPrepPrefix Longest Consecutive-Value Runs

Prefix Longest Consecutive-Value Runs

ZipRecruiter logoZipRecruiter● MediumNEW GRADOA
Learn

Problem statement

Process values from left to right. For every prefix, consider the set of distinct values seen so far and find the longest run of consecutive integers in that set.

Return one run length for every prefix. Repeated values do not extend a run.

Function

prefixConsecutiveRuns(values: int[]) → int[]

Examples

Example 1

values = [2,1,3]return = [1,2,3]

Each new value extends the only consecutive component.

Example 2

values = [3,1,2]return = [1,1,3]

The final value joins two singleton components.

Constraints

  • 1 <= values.length <= 2000
  • -1000000000 <= values[i] <= 1000000000

More ZipRecruiter problems

See ZipRecruiter hiring insights
public int[] prefixConsecutiveRuns(int[] values) {
    // Write your code here.
}
values[2,1,3]
expected[1,2,3]
Checking account…