从给定的对数组中按产品降序查找 K 个最大对的索引
给定一个包含N个整数对的数组arr[]和一个整数K ,任务是按乘积的降序找到K个最大对的索引。
例子:
Input: arr[] = {{9, 1}, {6, 3}, {6, 8}, {4, 5}, {1, 8}}, K = 1
Output: 2
Explanation: The pair with the largest product is at index 2 i.e, {6, 8}.
Input: arr[] = {{1, 2}, {1, 4}, {1, 3}, {1, 5}}, K = 4
Output: 3 1 2 0
方法:给定的问题可以使用贪心方法来解决。这个想法是将给定数组的每个给定对的(产品,索引)对存储到一个对的向量中,并按其乘积值的降序对对的向量进行排序。前K对的索引值是要求的答案。
下面是上述方法的实现:
C++
// C++ program of the above approach
#include
using namespace std;
// Function to find the index K largest
// product pairs in given array of pairs
void klargestProduct(vector > arr,
int K)
{
// Stores the size of arr
int N = arr.size();
// Stores the product and index
// pair for each pair in arr
vector > prod;
// Loop to traverse arr[]
for (int i = 0; i < N; i++) {
prod.push_back({ arr[i].first
* arr[i].second, i });
}
// Sort the array of pair in
// decreasing order of first
sort(prod.begin(), prod.end(),
greater >());
// Print Answer
for (int i = 0; i < K; i++) {
cout << prod[i].second << " ";
}
}
// Driver Code
int main()
{
vector > arr{
{ 9, 1 }, { 6, 3 }, { 6, 8 },
{ 4, 5 }, { 1, 8 }
};
int K = 1;
klargestProduct(arr, K);
return 0;
}
Java
// Java program of the above approach
import java.util.*;
class GFG {
static class pair implements Comparable {
int first, second;
pair(int s, int e) {
first = s;
second = e;
}
public int compareTo(pair p) {
return p.first - this.first;
}
}
// Function to find the index K largest
// product pairs in given array of pairs
static void klargestProduct(pair[] arr, int K) {
// Stores the size of arr
int N = arr.length;
// Stores the product and index
// pair for each pair in arr
Vector prod = new Vector();
// Loop to traverse arr[]
for (int i = 0; i < N; i++) {
prod.add(new pair(arr[i].first * arr[i].second, i));
}
// Sort the array of pair in
// decreasing order of first
Collections.sort(prod);
// Print Answer
for (int i = 0; i < K; i++) {
System.out.print(prod.get(i).second + " ");
}
}
// Driver Code
public static void main(String[] args) {
pair[] arr = { new pair(9, 1), new pair(6, 3),
new pair(6, 8), new pair(4, 5),
new pair(1, 8) };
int K = 1;
klargestProduct(arr, K);
}
}
// This code is contributed by 29AjayKumar
Python3
# Python program of the above approach
# Function to find the index K largest
# product pairs in given array of pairs
def klargestProduct(arr, K):
# Stores the size of arr
N = len(arr)
# Stores the product and index
# pair for each pair in arr
prod = []
# Loop to traverse arr[]
for i in range(N):
prod.append([arr[i][0] * arr[i][1], i])
# Sort the array of pair in
# decreasing order of first
prod = sorted(prod, key = lambda a:a[0], reverse = True)
# Print Answer
for i in range(K):
print(prod[i][1], end=" ")
# Driver Code
arr = [[9, 1], [6, 3], [6, 8], [4, 5], [1, 8]]
K = 1
klargestProduct(arr, K)
# This code is contributed by gfgking.
C#
// C# program of the above approach
using System;
using System.Collections.Generic;
public class GFG {
class pair : IComparable
{
public int first, second;
public pair(int first, int second)
{
this.first = first;
this.second = second;
}
public int CompareTo(pair p)
{
return p.first-this.first;
}
}
// Function to find the index K largest
// product pairs in given array of pairs
static void klargestProduct(pair[] arr, int K) {
// Stores the size of arr
int N = arr.Length;
// Stores the product and index
// pair for each pair in arr
List prod = new List();
// Loop to traverse []arr
for (int i = 0; i < N; i++) {
prod.Add(new pair(arr[i].first * arr[i].second, i));
}
// Sort the array of pair in
// decreasing order of first
prod.Sort();
// Print Answer
for (int i = 0; i < K; i++) {
Console.Write(prod[i].second + " ");
}
}
// Driver Code
public static void Main(String[] args) {
pair[] arr = { new pair(9, 1), new pair(6, 3),
new pair(6, 8), new pair(4, 5),
new pair(1, 8) };
int K = 1;
klargestProduct(arr, K);
}
}
// This code is contributed by shikhasingrajput
Javascript
输出
2
时间复杂度: O(N)
辅助空间: O(N)