2424. Longest Uploaded Prefix Medium

@problem@discussion
#Binary Search#Union Find#Design#Binary Indexed Tree#Segment Tree#Heap (Priority Queue)#Ordered Set



1/**
2 * [2424] Longest Uploaded Prefix
3 *
4 * 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.
5 * 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.<br />
6 * <br />
7 * Implement the LUPrefix class:
8 * 
9 * 	LUPrefix(int n) Initializes the object for a stream of n videos.
10 * 	void upload(int video) Uploads video to the server.
11 * 	int longest() Returns the length of the longest uploaded prefix defined above.
12 * 
13 *  
14 * Example 1:
15 * 
16 * Input
17 * ["LUPrefix", "upload", "longest", "upload", "longest", "upload", "longest"]
18 * [[4], [3], [], [1], [], [2], []]
19 * Output
20 * [null, null, 0, null, 1, null, 3]
21 * Explanation
22 * LUPrefix server = new LUPrefix(4);   // Initialize a stream of 4 videos.
23 * server.upload(3);                    // Upload video 3.
24 * server.longest();                    // Since video 1 has not been uploaded yet, there is no prefix.
25 *                                      // So, we return 0.
26 * server.upload(1);                    // Upload video 1.
27 * server.longest();                    // The prefix [1] is the longest uploaded prefix, so we return 1.
28 * server.upload(2);                    // Upload video 2.
29 * server.longest();                    // The prefix [1,2,3] is the longest uploaded prefix, so we return 3.
30 * 
31 *  
32 * Constraints:
33 * 
34 * 	1 <= n <= 10^5
35 * 	1 <= video <= n
36 * 	All values of video are distinct.
37 * 	At most 2 * 10^5 calls in total will be made to upload and longest.
38 * 	At least one call will be made to longest.
39 * 
40 */
41pub struct Solution {}
42
43// problem: https://leetcode.com/problems/longest-uploaded-prefix/
44// discuss: https://leetcode.com/problems/longest-uploaded-prefix/discuss/?currentPage=1&orderBy=most_votes&query=
45
46// submission codes start here
47
48struct LUPrefix {
49        false
50    }
51
52
53/** 
54 * `&self` means the method takes an immutable reference.
55 * If you need a mutable reference, change it to `&mut self` instead.
56 */
57impl LUPrefix {
58
59    fn new(n: i32) -> Self {
60        
61    }
62    
63    fn upload(&self, video: i32) {
64        
65    }
66    
67    fn longest(&self) -> i32 {
68        
69    }
70}
71
72/**
73 * Your LUPrefix object will be instantiated and called as such:
74 * let obj = LUPrefix::new(n);
75 * obj.upload(video);
76 * let ret_2: i32 = obj.longest();
77 */
78
79// submission codes end
80
81#[cfg(test)]
82mod tests {
83    use super::*;
84
85    #[test]
86    fn test_2424() {
87    }
88}
89


Back
© 2025 bowen.ge All Rights Reserved.