📌  相关文章
📜  从给定 3 个数组的任何排列中最大化增加的三元组的计数

📅  最后修改于: 2021-10-26 02:30:58             🧑  作者: Mango

给定三个数组X[]Y[]Z[],每个数组都由N 个整数组成,任务是找到三元组(X[i], Y[i], Z[i])的最大数量,例如( X[i] < Y[i] < Z[i])用于三个数组的任何排列。

例子:

朴素的方法:可以通过生成三个数组的三元组的所有可能组合并计算满足给定条件的三元组来解决给定的问题。检查所有排列后,打印获得的三元组的总数。

时间复杂度: O(N*(N!) 3 )
辅助空间: O(1)

高效方法:给定的问题可以通过使用贪婪方法来解决,其思想是对给定的数组X[]进行排序,然后为了找到三元组,选择数组Y[]Z[]中形成递增三元组的那些元素数组的每个元素和这个想法都可以使用优先级队列来实现。请按照以下步骤解决问题:

  • 按升序对数组X[]进行排序。
  • 初始化两个优先级队列,比如分别为数组Y[]Z[]实现 MinHeap 的PQYPQZ
  • 将数组Y[] 的所有元素存储在PQY 中
  • 将数组Z[] 的所有元素存储在PQZ 中
  • 遍历数组X[]并执行以下步骤:
    • 对于每个元素X[i] ,从优先级队列PQY 中弹出该元素,直到其顶部元素小于X[i]并且PQY不为空。
    • 对于PQY 的每个元素顶部元素,比如 temp,从优先级队列PQZ弹出元素,直到其顶部元素小于temp并且PQY不为空。
    • count的值增加1
  • 完成上述步骤后,打印count的值作为结果的最大三元组计数。

下面是上述方法的实现:

C++
// C++ program for the above approach
 
#include 
using namespace std;
 
// Function to find the number of triplets
// that are in increasing order from the
// given three arrays after rearranging
int countTriplet(int arr1[], int arr2[],
                 int arr3[], int N)
{
 
    // Sort the given array arr[]
    sort(arr1, arr1 + N);
 
    // Initializing priority queues
    priority_queue,
                   greater >
        Y;
    priority_queue,
                   greater >
        Z;
 
    // Push array elements arr2[i] in Y
    for (int i = 0; i < N; i++) {
        Y.push(arr2[i]);
    }
 
    // Push array elements arr3[i] in Y
    for (int i = 0; i < N; i++) {
        Z.push(arr3[i]);
    }
 
    int x, y, z;
    int ans = 0;
 
    // Traverse the array arr1[]
    for (int i = 0; i < N; i++) {
 
        x = arr1[i];
        while (!Y.empty()
               && Y.top() <= x)
            Y.pop();
 
        // If Y is empty then there is
        // no more triplets possible
        if (Y.empty())
            break;
 
        y = Y.top();
        Y.pop();
 
        while (!Z.empty()
               && Z.top() <= y)
            Z.pop();
 
        // If Z is empty then there is
        // no more triplets possible
        if (Z.empty())
            break;
 
        z = Z.top();
        Z.pop();
 
        // Increment the triplets count
        ++ans;
    }
 
    // Return the maximum count of triplets
    return ans;
}
 
// Driver Code
int main()
{
    int X[] = { 9, 6, 14, 1, 8 };
    int Y[] = { 2, 10, 3, 12, 11 };
    int Z[] = { 15, 13, 5, 7, 4 };
    int N = sizeof(X) / sizeof(X[0]);
 
    cout << countTriplet(X, Y, Z, N);
 
    return 0;
}


Java
// Java program for the above approach
import java.util.*;
public class Main
{
    // Function to find the number of triplets
    // that are in increasing order from the
    // given three arrays after rearranging
    static int countTriplet(int[] arr1, int[] arr2,
                     int[] arr3, int N)
    {
       
        // Sort the given array arr[]
        Arrays.sort(arr1);
       
        // Initializing priority queues
        Vector Y = new Vector();
        Vector Z = new Vector();
       
        // Push array elements arr2[i] in Y
        for (int i = 0; i < N; i++) {
            Y.add(arr2[i]);
        }
        // Push array elements arr3[i] in Y
        for (int i = 0; i < N; i++) {
            Z.add(arr3[i]);
        }
        Collections.sort(Y);
        Collections.sort(Z);
       
        int x, y, z;
        int ans = 0;
       
        // Traverse the array arr1[]
        for (int i = 0; i < N; i++) {
       
            x = arr1[i];
            while (Y.size() > 0 && Y.get(0) <= x)
                Y.remove(0);
       
            // If Y is empty then there is
            // no more triplets possible
            if (Y.size() == 0)
                break;
       
            y = Y.get(0);
            Y.remove(0);
       
            while (Z.size() > 0 && Z.get(0) <= y)
                Z.remove(0);
       
            // If Z is empty then there is
            // no more triplets possible
            if (Z.size() == 0)
                break;
       
            z = Z.get(0);
            Z.remove(0);
       
            // Increment the triplets count
            ++ans;
        }
       
        // Return the maximum count of triplets
        return ans;
    }
     
    public static void main(String[] args) {
        int[] X = { 9, 6, 14, 1, 8 };
        int[] Y = { 2, 10, 3, 12, 11 };
        int[] Z = { 15, 13, 5, 7, 4 };
        int N = X.length;
       
        System.out.println(countTriplet(X, Y, Z, N));
    }
}
 
// This code is contributed by suresh07.


Python3
# Python program for the above approach
from queue import PriorityQueue
 
# Function to find the number of triplets
# that are in increasing order from the
# given three arrays after rearranging
def countTriplet(arr1, arr2, arr3, N):
 
    # Sort the given array arr[]
    arr1.sort();
 
    # Initializing priority queues
    Y = PriorityQueue();
    Z = PriorityQueue();
 
    # Push array elements arr2[i] in Y
    for i in range(N):
        Y.put(arr2[i]);
     
 
    # Push array elements arr3[i] in Y
    for i in range(N):
        Z.put(arr3[i]);
 
    x = 0
    y = 0
    z = 0
    ans = 0;
 
    # Traverse the array arr1[]
    for i in range(N):
 
        x = arr1[i];
        while (not Y.empty() and Y.queue[0] <= x):
            Y.get();
 
        # If Y is empty then there is
        # no more triplets possible
        if (Y.empty()):
            break;
 
        y = Y.queue[0];
        Y.get()
 
        while (not Z.empty() and Z.queue[0] <= y):
            Z.get();
 
        # If Z is empty then there is
        # no more triplets possible
        if (Z.empty()):
            break;
 
        z = Z.queue[0];
        Z.get();
 
        # Increment the triplets count
        ans += 1;
 
    # Return the maximum count of triplets
    return ans;
 
# Driver Code
X = [ 9, 6, 14, 1, 8 ];
Y = [ 2, 10, 3, 12, 11 ];
Z = [ 15, 13, 5, 7, 4 ];
N = len(X);
 
print(countTriplet(X, Y, Z, N));
 
# This code is contributed by _saurabh_jaiswal.


C#
// C# program for the above approach
using System;
using System.Collections.Generic;
class GFG {
     
    // Function to find the number of triplets
    // that are in increasing order from the
    // given three arrays after rearranging
    static int countTriplet(int[] arr1, int[] arr2,
                     int[] arr3, int N)
    {
      
        // Sort the given array arr[]
        Array.Sort(arr1);
      
        // Initializing priority queues
        List Y = new List();
        List Z = new List();
      
        // Push array elements arr2[i] in Y
        for (int i = 0; i < N; i++) {
            Y.Add(arr2[i]);
        }
        // Push array elements arr3[i] in Y
        for (int i = 0; i < N; i++) {
            Z.Add(arr3[i]);
        }
        Y.Sort();
        Z.Sort();
      
        int x, y, z;
        int ans = 0;
      
        // Traverse the array arr1[]
        for (int i = 0; i < N; i++) {
      
            x = arr1[i];
            while (Y.Count > 0
                   && Y[0] <= x)
                Y.RemoveAt(0);
      
            // If Y is empty then there is
            // no more triplets possible
            if (Y.Count == 0)
                break;
      
            y = Y[0];
            Y.RemoveAt(0);
      
            while (Z.Count > 0
                   && Z[0] <= y)
                Z.RemoveAt(0);
      
            // If Z is empty then there is
            // no more triplets possible
            if (Z.Count == 0)
                break;
      
            z = Z[0];
            Z.RemoveAt(0);
      
            // Increment the triplets count
            ++ans;
        }
      
        // Return the maximum count of triplets
        return ans;
    }
 
  static void Main() {
    int[] X = { 9, 6, 14, 1, 8 };
    int[] Y = { 2, 10, 3, 12, 11 };
    int[] Z = { 15, 13, 5, 7, 4 };
    int N = X.Length;
  
    Console.Write(countTriplet(X, Y, Z, N));
  }
}
 
// This code is contributed by divyeshrabadiya07.


Javascript


输出:
3

时间复杂度: O(N * log N)
辅助空间: O(N)

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程