← 返回 google 的题目列表Belt Packing: Return a Triplet When 3 Items Fit a Threshold
类型:online_judge
Problem: Conveyor Belt Packing (Threshold Condition, Online Triplet Output)
A conveyor belt produces items continuously. Each item has an integer size. Whenever there exist 3 items a, b, c satisfying:
max(a,b,c) - min(a,b,c) < threshold
those three items can be packed together.
Implement a class Packer:
Initialized with threshold.
Method add(x) inserts a new item of size x.
If after insertion there exists any valid triplet, add must immediately return one such triplet (sorted ascending) and remove those three items from the system.
Otherwise return empty.
Constraints
1 <= threshold <= 1e9
0 <= x <= 1e9
Up to N = 2e5 calls to add.
Example
threshold = 3, calling add(5), add(6), add(7) returns [5,6,7] on the third call since 7-5=2 < 3.
You may return any valid triplet if multiple exist.
Example
Input
threshold=3, add sequence: 5,6,7
Output
[5,6,7] (on 3rd add)