Skip to content

Practice Longest Uploaded Prefix

Read the task, work through the examples, and check your approach against the constraints. Use the original LeetCode task for coding and submissions.

Problem Statement

LUPrefix(n) tracks video IDs from 1 through n. upload(video) marks an ID uploaded. longest() returns the largest i such that every ID from 1 through i is uploaded, or zero if ID 1 is missing. Upload IDs are distinct. Practice approach: Store a boolean presence array and a prefix pointer. After an upload, move the pointer while its next ID is present. Each pointer position is visited once, so total advancement is O(n) and longest is O(1). The largest uploaded ID alone is not the prefix length.

Source: Longest Uploaded Prefix on LeetCode. Use the original task for its coding environment and submissions.

Examples

# Example 1

Input
["LUPrefix","upload","longest","upload","longest","upload","longest"]
[[3],[2],[],[3],[],[1],[]]
Output
[null,null,0,null,0,null,3]
Explanation

IDs 2 and 3 do not form a prefix until ID 1 arrives.

Constraints

1 <= n <= 10^5
1 <= video <= n
Upload IDs are distinct.
At most 2 * 10^5 total calls and at least one longest call.

Related LeetCode Problems

Practice more problems similar to Longest Uploaded Prefix.

Review your practice with PhantomCodeAI

After trying the problem yourself, use the app to discuss your approach, complexity, and edge cases.