Product of the Maximum and Minimum in a Dataset
Starting with an empty set of integers named elements, perform the following query operations:
- The command
push xinserts the value ofxintoelements. - The command
pop xremoves the value ofxfromelements.
The integers in elements need to be ordered in such a way that after each operation is performed, the product of the maximum and minimum values in the set can be easily calculated.
Complete the function maxMin in the editor below.
maxMin has the following parameter(s):
s[n]: an array of operations stringsint x[n]: an array ofxwherex[i]goes withoperations[i].
Returns
int[n]: an array of long integers that denote the product of the maximum and minimum of elements after each query
1Example 1
Visualize elements as an empty multiset, elements = {},
and refer to the return array as products. The sequence of operations occurs as follows:
- push 1 → elements = {1}, so the minimum = 1 and the maximum = 1.
Then store the product as
products[0] = 1 × 1 = 1. - push 2 → elements = {1, 2}, so the minimum = 1 and the maximum = 2.
Then store the product as
products[1] = 1 × 2 = 2. - push 3 → elements = {1, 2, 3}, so the minimum = 1 and the maximum = 3.
Then store the product as
products[2] = 1 × 3 = 3. - pop 1 → elements = {2, 3}, so the minimum = 2 and the maximum = 3.
Then store the product as
products[3] = 2 × 3 = 6.
Return products = [1, 2, 3, 6]
Constraints
Limits and guarantees your solution can rely on.
1 ≤ n ≤ 10^51 ≤ x[i] ≤ 10^9- It is guaranteed that each
operations[i]is either push or pop. - It is guaranteed that any value popped will exist in the array.