Skip to content

Commit 30c740f

Browse files
committed
+ problem 2424
1 parent 104958c commit 30c740f

File tree

5 files changed

+205
-0
lines changed

5 files changed

+205
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
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+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
# 2424. 最长上传前缀
2+
给你一个 `n` 个视频的上传序列,每个视频编号为 `1``n` 之间的 **不同** 数字,你需要依次将这些视频上传到服务器。请你实现一个数据结构,在上传的过程中计算 **最长上传前缀**
3+
4+
如果 **闭区间** `1``i` 之间的视频全部都已经被上传到服务器,那么我们称 `i` 是上传前缀。最长上传前缀指的是符合定义的 `i` 中的 **最大值**
5+
6+
请你实现 `LUPrefix` 类:
7+
* `LUPrefix(int n)` 初始化一个 `n` 个视频的流对象。
8+
* `void upload(int video)` 上传 `video` 到服务器。
9+
* `int longest()` 返回上述定义的 **最长上传前缀** 的长度。
10+
11+
#### 示例 1:
12+
<pre>
13+
<strong>输入:</strong>
14+
["LUPrefix", "upload", "longest", "upload", "longest", "upload", "longest"]
15+
[[4], [3], [], [1], [], [2], []]
16+
<strong>输出:</strong>
17+
[null, null, 0, null, 1, null, 3]
18+
<strong>解释:</strong>
19+
LUPrefix server = new LUPrefix(4); // 初始化 4个视频的上传流
20+
server.upload(3); // 上传视频 3 。
21+
server.longest(); // 由于视频 1 还没有被上传,最长上传前缀是 0 。
22+
server.upload(1); // 上传视频 1 。
23+
server.longest(); // 前缀 [1] 是最长上传前缀,所以我们返回 1 。
24+
server.upload(2); // 上传视频 2 。
25+
server.longest(); // 前缀 [1,2,3] 是最长上传前缀,所以我们返回 3 。
26+
</pre>
27+
28+
#### 提示:
29+
* <code>1 <= n <= 10<sup>5</sup></code>
30+
* `1 <= video <= n`
31+
* `video` 中所有值 **互不相同**
32+
* `upload``longest` **总调用** 次数至多不超过 <code>2 * 10<sup>5</sup></code> 次。
33+
* 至少会调用 `longest` 一次。
34+
35+
## 题解 (Rust)
36+
37+
### 1. 题解
38+
```Rust
39+
use std::cmp::Reverse;
40+
use std::collections::BinaryHeap;
41+
42+
struct LUPrefix {
43+
heap: BinaryHeap<Reverse<i32>>,
44+
longest_uploaded_prefix: i32,
45+
}
46+
47+
/**
48+
* `&self` means the method takes an immutable reference.
49+
* If you need a mutable reference, change it to `&mut self` instead.
50+
*/
51+
impl LUPrefix {
52+
fn new(n: i32) -> Self {
53+
Self {
54+
heap: BinaryHeap::new(),
55+
longest_uploaded_prefix: 0,
56+
}
57+
}
58+
59+
fn upload(&mut self, video: i32) {
60+
self.heap.push(Reverse(video));
61+
}
62+
63+
fn longest(&mut self) -> i32 {
64+
while self.heap.peek().unwrap_or(&Reverse(0)).0 == self.longest_uploaded_prefix + 1 {
65+
self.heap.pop();
66+
self.longest_uploaded_prefix += 1;
67+
}
68+
69+
self.longest_uploaded_prefix
70+
}
71+
}
72+
73+
/**
74+
* Your LUPrefix object will be instantiated and called as such:
75+
* let obj = LUPrefix::new(n);
76+
* obj.upload(video);
77+
* let ret_2: i32 = obj.longest();
78+
*/
79+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
use std::cmp::Reverse;
2+
use std::collections::BinaryHeap;
3+
4+
struct LUPrefix {
5+
heap: BinaryHeap<Reverse<i32>>,
6+
longest_uploaded_prefix: i32,
7+
}
8+
9+
/**
10+
* `&self` means the method takes an immutable reference.
11+
* If you need a mutable reference, change it to `&mut self` instead.
12+
*/
13+
impl LUPrefix {
14+
fn new(n: i32) -> Self {
15+
Self {
16+
heap: BinaryHeap::new(),
17+
longest_uploaded_prefix: 0,
18+
}
19+
}
20+
21+
fn upload(&mut self, video: i32) {
22+
self.heap.push(Reverse(video));
23+
}
24+
25+
fn longest(&mut self) -> i32 {
26+
while self.heap.peek().unwrap_or(&Reverse(0)).0 == self.longest_uploaded_prefix + 1 {
27+
self.heap.pop();
28+
self.longest_uploaded_prefix += 1;
29+
}
30+
31+
self.longest_uploaded_prefix
32+
}
33+
}
34+
35+
/**
36+
* Your LUPrefix object will be instantiated and called as such:
37+
* let obj = LUPrefix::new(n);
38+
* obj.upload(video);
39+
* let ret_2: i32 = obj.longest();
40+
*/

README.md

+3
Original file line numberDiff line numberDiff line change
@@ -1594,6 +1594,7 @@
15941594
[2419][2419l]|[Longest Subarray With Maximum Bitwise AND][2419] |![rs]
15951595
[2420][2420l]|[Find All Good Indices][2420] |![rs]
15961596
[2423][2423l]|[Remove Letter To Equalize Frequency][2423] |![rs]
1597+
[2424][2424l]|[Longest Uploaded Prefix][2424] |![rs]
15971598
[2425][2425l]|[Bitwise XOR of All Pairings][2425] |![rs]
15981599
[2426][2426l]|[Number of Pairs Satisfying Inequality][2426] |![rs]
15991600
[2427][2427l]|[Number of Common Factors][2427] |![rs]
@@ -3308,6 +3309,7 @@
33083309
[2419]:Problemset/2419-Longest%20Subarray%20With%20Maximum%20Bitwise%20AND/README.md#2419-longest-subarray-with-maximum-bitwise-and
33093310
[2420]:Problemset/2420-Find%20All%20Good%20Indices/README.md#2420-find-all-good-indices
33103311
[2423]:Problemset/2423-Remove%20Letter%20To%20Equalize%20Frequency/README.md#2423-remove-letter-to-equalize-frequency
3312+
[2424]:Problemset/2424-Longest%20Uploaded%20Prefix/README.md#2424-longest-uploaded-prefix
33113313
[2425]:Problemset/2425-Bitwise%20XOR%20of%20All%20Pairings/README.md#2425-bitwise-xor-of-all-pairings
33123314
[2426]:Problemset/2426-Number%20of%20Pairs%20Satisfying%20Inequality/README.md#2426-number-of-pairs-satisfying-inequality
33133315
[2427]:Problemset/2427-Number%20of%20Common%20Factors/README.md#2427-number-of-common-factors
@@ -5016,6 +5018,7 @@
50165018
[2419l]:https://leetcode.com/problems/longest-subarray-with-maximum-bitwise-and/
50175019
[2420l]:https://leetcode.com/problems/find-all-good-indices/
50185020
[2423l]:https://leetcode.com/problems/remove-letter-to-equalize-frequency/
5021+
[2424l]:https://leetcode.com/problems/longest-uploaded-prefix/
50195022
[2425l]:https://leetcode.com/problems/bitwise-xor-of-all-pairings/
50205023
[2426l]:https://leetcode.com/problems/number-of-pairs-satisfying-inequality/
50215024
[2427l]:https://leetcode.com/problems/number-of-common-factors/

README_CN.md

+3
Original file line numberDiff line numberDiff line change
@@ -1594,6 +1594,7 @@
15941594
[2419][2419l]|[按位与最大的最长子数组][2419] |![rs]
15951595
[2420][2420l]|[找到所有好下标][2420] |![rs]
15961596
[2423][2423l]|[删除字符使频率相同][2423] |![rs]
1597+
[2424][2424l]|[最长上传前缀][2424] |![rs]
15971598
[2425][2425l]|[所有数对的异或和][2425] |![rs]
15981599
[2426][2426l]|[满足不等式的数对数目][2426] |![rs]
15991600
[2427][2427l]|[公因子的数目][2427] |![rs]
@@ -3308,6 +3309,7 @@
33083309
[2419]:Problemset/2419-Longest%20Subarray%20With%20Maximum%20Bitwise%20AND/README_CN.md#2419-按位与最大的最长子数组
33093310
[2420]:Problemset/2420-Find%20All%20Good%20Indices/README_CN.md#2420-找到所有好下标
33103311
[2423]:Problemset/2423-Remove%20Letter%20To%20Equalize%20Frequency/README_CN.md#2423-删除字符使频率相同
3312+
[2424]:Problemset/2424-Longest%20Uploaded%20Prefix/README_CN.md#2424-最长上传前缀
33113313
[2425]:Problemset/2425-Bitwise%20XOR%20of%20All%20Pairings/README_CN.md#2425-所有数对的异或和
33123314
[2426]:Problemset/2426-Number%20of%20Pairs%20Satisfying%20Inequality/README_CN.md#2426-满足不等式的数对数目
33133315
[2427]:Problemset/2427-Number%20of%20Common%20Factors/README_CN.md#2427-公因子的数目
@@ -5016,6 +5018,7 @@
50165018
[2419l]:https://leetcode.cn/problems/longest-subarray-with-maximum-bitwise-and/
50175019
[2420l]:https://leetcode.cn/problems/find-all-good-indices/
50185020
[2423l]:https://leetcode.cn/problems/remove-letter-to-equalize-frequency/
5021+
[2424l]:https://leetcode.cn/problems/longest-uploaded-prefix/
50195022
[2425l]:https://leetcode.cn/problems/bitwise-xor-of-all-pairings/
50205023
[2426l]:https://leetcode.cn/problems/number-of-pairs-satisfying-inequality/
50215024
[2427l]:https://leetcode.cn/problems/number-of-common-factors/

0 commit comments

Comments
 (0)