|
| 1 | +# 2424. Longest Uploaded Prefix |
| 2 | +You are given a stream of `n` videos, each represented by a **distinct** number from `1` to `n` that you need to "upload" to a server. You need to implement a data structure that calculates the length of the **longest uploaded prefix** at various points in the upload process. |
| 3 | + |
| 4 | +We consider `i` to be an uploaded prefix if all videos in the range `1` to `i` (**inclusive**) have been uploaded to the server. The longest uploaded prefix is the **maximum** value of `i` that satisfies this definition. |
| 5 | + |
| 6 | +Implement the `LUPrefix` class: |
| 7 | +* `LUPrefix(int n)` Initializes the object for a stream of `n` videos. |
| 8 | +* `void upload(int video)` Uploads `video` to the server. |
| 9 | +* `int longest()` Returns the length of the **longest uploaded prefix** defined above. |
| 10 | + |
| 11 | +#### Example 1: |
| 12 | +<pre> |
| 13 | +<strong>Input:</strong> |
| 14 | +["LUPrefix", "upload", "longest", "upload", "longest", "upload", "longest"] |
| 15 | +[[4], [3], [], [1], [], [2], []] |
| 16 | +<strong>Output:</strong> |
| 17 | +[null, null, 0, null, 1, null, 3] |
| 18 | +<strong>Explanation:</strong> |
| 19 | +LUPrefix server = new LUPrefix(4); // Initialize a stream of 4 videos. |
| 20 | +server.upload(3); // Upload video 3. |
| 21 | +server.longest(); // Since video 1 has not been uploaded yet, there is no prefix. |
| 22 | + // So, we return 0. |
| 23 | +server.upload(1); // Upload video 1. |
| 24 | +server.longest(); // The prefix [1] is the longest uploaded prefix, so we return 1. |
| 25 | +server.upload(2); // Upload video 2. |
| 26 | +server.longest(); // The prefix [1,2,3] is the longest uploaded prefix, so we return 3. |
| 27 | +</pre> |
| 28 | + |
| 29 | +#### Constraints: |
| 30 | +* <code>1 <= n <= 10<sup>5</sup></code> |
| 31 | +* `1 <= video <= n` |
| 32 | +* All values of `video` are **distinct**. |
| 33 | +* At most <code>2 * 10<sup>5</sup></code> calls **in total** will be made to `upload` and `longest`. |
| 34 | +* At least one call will be made to `longest`. |
| 35 | + |
| 36 | +## Solutions (Rust) |
| 37 | + |
| 38 | +### 1. Solution |
| 39 | +```Rust |
| 40 | +use std::cmp::Reverse; |
| 41 | +use std::collections::BinaryHeap; |
| 42 | + |
| 43 | +struct LUPrefix { |
| 44 | + heap: BinaryHeap<Reverse<i32>>, |
| 45 | + longest_uploaded_prefix: i32, |
| 46 | +} |
| 47 | + |
| 48 | +/** |
| 49 | + * `&self` means the method takes an immutable reference. |
| 50 | + * If you need a mutable reference, change it to `&mut self` instead. |
| 51 | + */ |
| 52 | +impl LUPrefix { |
| 53 | + fn new(n: i32) -> Self { |
| 54 | + Self { |
| 55 | + heap: BinaryHeap::new(), |
| 56 | + longest_uploaded_prefix: 0, |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + fn upload(&mut self, video: i32) { |
| 61 | + self.heap.push(Reverse(video)); |
| 62 | + } |
| 63 | + |
| 64 | + fn longest(&mut self) -> i32 { |
| 65 | + while self.heap.peek().unwrap_or(&Reverse(0)).0 == self.longest_uploaded_prefix + 1 { |
| 66 | + self.heap.pop(); |
| 67 | + self.longest_uploaded_prefix += 1; |
| 68 | + } |
| 69 | + |
| 70 | + self.longest_uploaded_prefix |
| 71 | + } |
| 72 | +} |
| 73 | + |
| 74 | +/** |
| 75 | + * Your LUPrefix object will be instantiated and called as such: |
| 76 | + * let obj = LUPrefix::new(n); |
| 77 | + * obj.upload(video); |
| 78 | + * let ret_2: i32 = obj.longest(); |
| 79 | + */ |
| 80 | +``` |
0 commit comments