给定Q查询。查询分为以下三种类型:
- 将数字num添加到列表中。
- 从列表中删除数字num 。
- 返回列表中最大值和最小值之间的差。
任务是编写一个执行上述查询的程序。
注意:数字是不同的,并且每次调用query-3时,列表中至少要包含1个元素。
例子:
Input:
Q = 5
Query of type 1: num = 3
Query of type 1: num = 5
Query of type 1: num = 6
Query of type 2: num = 6
Query of type 1: num = 2
Query of type 3:
Output: 4
Since query of type 3 has been called once only, the answer at the instant is 4.
After first query of type-1, the list is {3}
After second query of type-1, the list is {3, 5}
After third query of type-1, the list is {3, 5, 6}
After fourth query of type-2, the list is {3, 5}
After fifth query of type-1, the list is {2, 3, 5}
On sixth query of type-3, the answer is 5-2.
一个简单的解决方案是按照以下步骤操作:
- 将数字存储在向量数组中。
- 对于类型1的查询,将一个元素添加到数组中。
- 对于类型2的查询,请从向量或数组中删除该元素。
- 对于类型3的查询,遍历数组并找到数组中的最小值和最大值,并返回它们之间的差。
时间复杂度: O(1)用于插入类型2的查询,O(N)用于插入类型2的查询(在最坏的情况下),O(N)用于插入类型3的查询。
辅助空间: O(N)
一种有效的解决方案是使用自平衡二进制搜索树(在C++中作为集合容器实现,在Java作为TreeSet实现)。请按照以下步骤解决上述问题。
- 使用insert()函数将元素插入容器。
- 使用delete()函数从容器中删除元素。
- 最小值始终在开头,最大值始终在结尾。可以使用begin()和rbegin()函数检索它们。
下面是有效方法的实现:
C++
// C++ program to perform Queries to
// add, remove and return
// the difference of maximum and minimum.
#include
using namespace std;
set s;
// function to perform query 1
void performQueryOne(int num)
{
// insert the element
s.insert(num);
}
// function to perform query 2
void performQueryTwo(int num)
{
// erase the element
s.erase(num);
}
// function to perform query 3
int performQueryThree()
{
int mini = *(s.begin());
int maxi = *(s.rbegin());
return maxi - mini;
}
// Driver Code
int main()
{
// query type-1
int num = 3;
performQueryOne(num);
// query type-1
num = 5;
performQueryOne(num);
// query type-1
num = 6;
performQueryOne(num);
// query type-2
num = 5;
performQueryTwo(num);
// query type-1
num = 2;
performQueryOne(num);
// query type-3
cout << performQueryThree();
return 0;
}
Java
// Java program to perform Queries
// to add, remove and return
// the difference of maximum and
// minimum using TreeSet.
import java.util.*;
class GFG
{
static SortedSet t = new TreeSet();
// function to perform query 1
static void performQueryOne(int num)
{
// insert the element
t.add(num);
}
// function to perform query 2
static void performQueryTwo(int num)
{
// erase the element
t.remove(num);
}
// function to perform query 3
static int performQueryThree()
{
int mini = t.first();
int maxi = t.last();
return maxi - mini;
}
// Driver Code
public static void main(String[] args)
{
// query type-1
int num = 3;
performQueryOne(num);
// query type-1
num = 5;
performQueryOne(num);
// query type-1
num = 6;
performQueryOne(num);
// query type-2
num = 5;
performQueryTwo(num);
// query type-1
num = 2;
performQueryOne(num);
// query type-3
System.out.println(performQueryThree());
}
}
// This code is contributed by debjitdbb
Python3
# Python3 program to perform Queries
# to add, remove and return
# difference of maximum and minimum.
# Function to perform query 1
def performQueryOne(num):
# insert the element
s.add(num)
# Function to perform query 2
def performQueryTwo(num):
# erase the element
s.remove(num)
# function to perform query 3
def performQueryThree():
mini = min(s)
maxi = max(s)
return maxi - mini
# Driver Code
if __name__ == "__main__":
s = set()
# query type-1
num = 3
performQueryOne(num)
# query type-1
num = 5
performQueryOne(num)
# query type-1
num = 6
performQueryOne(num)
# query type-2
num = 5
performQueryTwo(num)
# query type-1
num = 2
performQueryOne(num)
# query type-3
print(performQueryThree())
# This code is contributed by Rituraj Jain
C#
// C# program to perform Queries
// to add, remove and return
// the difference of maximum and
// minimum using TreeSet.
using System;
using System.Collections.Generic;
class GFG
{
static SortedSet t = new SortedSet();
// function to perform query 1
static void performQueryOne(int num)
{
// insert the element
t.Add(num);
}
// function to perform query 2
static void performQueryTwo(int num)
{
// erase the element
t.Remove(num);
}
// function to perform query 3
static int performQueryThree()
{
int mini = t.Min;
int maxi = t.Max;
return maxi - mini;
}
// Driver Code
public static void Main(String[] args)
{
// query type-1
int num = 3;
performQueryOne(num);
// query type-1
num = 5;
performQueryOne(num);
// query type-1
num = 6;
performQueryOne(num);
// query type-2
num = 5;
performQueryTwo(num);
// query type-1
num = 2;
performQueryOne(num);
// query type-3
Console.WriteLine(performQueryThree());
}
}
// This code is contributed by 29AjayKumar
4
时间复杂度:每个查询为O(log N)。
辅助空间: O(N)
如果您希望与行业专家一起参加现场课程,请参阅《 Geeks现场课程》和《 Geeks现场课程美国》。