java.lang.Object
g2401_2500.s2424_longest_uploaded_prefix.LUPrefix

public class LUPrefix extends Object
2424 - Longest Uploaded Prefix.<p>Medium</p> <p>You are given a stream of <code>n</code> videos, each represented by a <strong>distinct</strong> number from <code>1</code> to <code>n</code> that you need to &ldquo;upload&rdquo; to a server. You need to implement a data structure that calculates the length of the <strong>longest uploaded prefix</strong> at various points in the upload process.</p> <p>We consider <code>i</code> to be an uploaded prefix if all videos in the range <code>1</code> to <code>i</code> ( <strong>inclusive</strong> ) have been uploaded to the server. The longest uploaded prefix is the <strong>maximum</strong> value of <code>i</code> that satisfies this definition.</p> <p>Implement the <code>LUPrefix</code> class:</p> <ul> <li><code>LUPrefix(int n)</code> Initializes the object for a stream of <code>n</code> videos.</li> <li><code>void upload(int video)</code> Uploads <code>video</code> to the server.</li> <li><code>int longest()</code> Returns the length of the <strong>longest uploaded prefix</strong> defined above.</li> </ul> <p><strong>Example 1:</strong></p> <p><strong>Input</strong></p> <p>[&ldquo;LUPrefix&rdquo;, &ldquo;upload&rdquo;, &ldquo;longest&rdquo;, &ldquo;upload&rdquo;, &ldquo;longest&rdquo;, &ldquo;upload&rdquo;, &ldquo;longest&rdquo;]</p> <p>[[4], [3], [], [1], [], [2], []]</p> <p><strong>Output:</strong> [null, null, 0, null, 1, null, 3]</p> <p><strong>Explanation:</strong></p> <pre><code> LUPrefix server = new LUPrefix(4); // Initialize a stream of 4 videos. server.upload(3); // Upload video 3. server.longest(); // Since video 1 has not been uploaded yet, there is no prefix. // So, we return 0. server.upload(1); // Upload video 1. server.longest(); // The prefix [1] is the longest uploaded prefix, so we return 1. server.upload(2); // Upload video 2. server.longest(); // The prefix [1,2,3] is the longest uploaded prefix, so we return 3. </code></pre> <p><strong>Constraints:</strong></p> <ul> <li><code>1 <= n <= 10<sup>5</sup></code></li> <li><code>1 <= video <= n</code></li> <li>All values of <code>video</code> are <strong>distinct</strong>.</li> <li>At most <code>2 * 10<sup>5</sup></code> calls <strong>in total</strong> will be made to <code>upload</code> and <code>longest</code>.</li> <li>At least one call will be made to <code>longest</code>.</li> </ul>
  • Constructor Details

    • LUPrefix

      public LUPrefix(int n)
  • Method Details

    • upload

      public void upload(int video)
    • longest

      public int longest()