给定三个数组firstkey [] , secondkey []和value []其中元素firstkey [i]和secondkey [i]表示一个组合键,它们的值是value [i] ,任务是回答Q个查询,每个查询都有两个元素表示组合键,需要打印其相应的值。
例子:
Input: firstkey[] = {4, 4, 5}
secondkey[] = {2, 1, 3}
value[] = {5, 3, 8}, Q = {(4, 1)}
Output: 3
Explanation:
The composite key is present at firstkey[1] (= 4) and secondkey[1] (= 1)
Therefore, the corresponding value is value[1] = 3
Input: firstkey[] = {3, 4, 3}
secondkey[] = {7, 1, 3}
value[] = {2, 3, 6}, Q = {(3, 3)}
Output: 6
Explanation:
The composite key is present at firstkey[2] (= 3) and secondkey[2] (= 3)
Therefore, the corresponding value is value[2] = 6
方法:想法是使用map,其中map的键是C++中的两个整数对, Python的tuple表示被映射到相应value []元素的firstkey []和secondkey []的各个元素。这使我们能够在O(1)时间内回答每个查询。
例如:
Given arrays be -
firstkey[] = {4, 4, 5}
secondkey[] = {7, 1, 3}
value[] = {5, 3, 8}
Iterating over the Array, the map thus
formed is {(4, 7): 5, (4, 1): 3, (5, 3): 8}
下面是上述方法的实现:
C++
// C++ implementation to find the
// value of the given composite keys
#include
using namespace std;
// Function to find the composite
// key value for multiple queries
void findCompositeKey(int firstkey[],
int secondkey[], int value[], int n,
vector > q)
{
// Map to store the composite
// key with a value
map, int> M;
// Iterate over the arrays
// and generate the required
// mappings
for (int i = 0; i < n; i++) {
M.insert({ { firstkey[i], secondkey[i] },
value[i] });
}
// Loop to iterate over the
// elements of the queries
for (auto i : q) {
// Condition to check if there
// is the composite key in map
if (M.find({ i.first,
i.second })
!= M.end()) {
cout << M[{ i.first, i.second }]
<< endl;
}
else {
cout << "Not Found" << endl;
}
}
}
// Driver Code
int main()
{
int firstkey[] = { 4, 4, 5 };
int secondkey[] = { 2, 1, 3 };
int value[] = { 5, 3, 8 };
int n = 3;
vector > q = { { 4, 1 },
{ 5, 2 },
{ 5, 3 } };
findCompositeKey(firstkey, secondkey,
value, n, q);
return 0;
}
Java
// Java implementation to find the
// value of the given composite keys
import java.util.*;
class GFG{
static class Pair
{
int first, second;
Pair(int first, int second)
{
this.first = first;
this.second = second;
}
@Override
public boolean equals(Object obj)
{
if (obj == null)
return false;
if (!(obj instanceof Pair))
return false;
if (obj == this)
return true;
return (this.first == ((Pair)obj).first) &&
(this.second == ((Pair)obj).second);
}
@Override
public int hashCode()
{
return this.first + this.second;
}
}
// Function to find the composite
// key value for multiple queries
static void findCompositeKey(int firstkey[],
int secondkey[],
int value[], int n,
int[][] q)
{
// Map to store the composite
// key with a value
Map M = new HashMap<>();
// Iterate over the arrays
// and generate the required
// mappings
for(int i = 0; i < n; i++)
{
M.put(new Pair(firstkey[i],
secondkey[i]),
value[i]);
}
// Loop to iterate over the
// elements of the queries
for(int i = 0; i < q.length; i++)
{
// Condition to check if there
// is the composite key in map
if (M.containsKey(new Pair(q[i][0],
q[i][1])))
{
System.out.println(M.get(new Pair(q[i][0],
q[i][1])));
}
else
{
System.out.println("Not Found");
}
}
}
// Driver code
public static void main(String[] args)
{
int firstkey[] = { 4, 4, 5 };
int secondkey[] = { 2, 1, 3 };
int value[] = { 5, 3, 8 };
int n = 3;
int[][] q = { { 4, 1 },
{ 5, 2 },
{ 5, 3 } };
findCompositeKey(firstkey, secondkey,
value, n, q);
}
}
// This code is contributed by offbeat
3
Not Found
8
如果您希望与行业专家一起参加现场课程,请参阅《 Geeks现场课程》和《 Geeks现场课程美国》。