📌  相关文章
📜  通过添加另一个数组中的所有元素,检查数组元素是否可以最大化到M

📅  最后修改于: 2021-05-17 01:42:37             🧑  作者: Mango

给定正整数M以及分别为NK个正整数的两个数组arr []value [] ,任务是将value []中的每个元素添加到arr []中的元素,以便在执行所有加法之后,数组中的最大元素最多为M。如果可能的话,请打印“是” 。否则,打印“否”
例子:

天真的方法:
最简单的方法是从给定数组arr []中选择任何K个元素,然后将K个值与选择的K个值相加到value []数组中。可以将这K个值添加到K中数组arr []K个选定数字中!方式(在最坏的情况下)。
时间复杂度: O( N P K )
高效方法:
请按照以下步骤解决问题:

  1. 按降序对value []数组中的元素进行排序。
  2. arr []的所有元素存储在最小优先级队列中。
  3. 现在从优先级队列中提取最小元素(例如X ),并将数组value []中的元素添加到X中
  4. 从数组value []X的相加超过M时,然后将元素X插入优先级队列,并对优先级队列中的下一个最小值重复上述步骤。
  5. 如果将value []中的所有元素都添加到arr []中的某些元素中,则为“是” ,否则打印“否”

下面是上述方法的实现:

C++
// C++ Program to implement the
// above approach
 
#include 
using namespace std;
 
// Function which checks if all
// additions are possible
void solve(int ar[], int values[],
        int N, int K, int M)
{
 
    // Sorting values[] in
    // decreasing order
    sort(values, values + K,
        greater());
 
    // Minimum priority queue which
    // contains all the elements
    // of array arr[]
    priority_queue,
                greater >
        pq;
 
    for (int x = 0; x < N; x++) {
        pq.push(ar[x]);
    }
 
    // poss stores whether all the
    // additions are possible
    bool poss = true;
    for (int x = 0; x < K; x++) {
 
        // Minium value in the
        // priority queue
        int mini = pq.top();
        pq.pop();
        int val = mini + values[x];
 
        // If on addition it exceeds
        // M then not possible
        if (val > M) {
            poss = false;
            break;
        }
        pq.push(val);
    }
 
    // If all elements are added
    if (poss) {
        cout << "Yes" << endl;
    }
    else {
        cout << "No" << endl;
    }
}
 
// Driver Code
int main()
{
    int ar[] = { 5, 9, 3, 8, 7 };
    int N = 5;
 
    int values[] = { 1, 2, 3, 4 };
    int K = 4;
 
    int M = 9;
 
    solve(ar, values, N, K, M);
    return 0;
}


Java
// Java program to implement the
// above approach
import java.io.*;
import java.util.*;
 
class GFG{
            
// Function which checks if all
// additions are possible
static void solve(Integer ar[], Integer values[],
                  int N, int K, int M)
{
     
    // Sorting values[] in
    // decreasing order
    Arrays.sort(values, (a, b) -> b - a);
 
    // Minimum priority queue which
    // contains all the elements
    // of array arr[]
    PriorityQueue pq = new PriorityQueue<>();
     
    for(int x = 0; x < N; x++)
    {
        pq.add(ar[x]);
    }
     
    // poss stores whether all the
    // additions are possible
    boolean poss = true;
    for(int x = 0; x < K; x++)
    {
         
        // Minium value in the
        // priority queue
        int mini = pq.peek();
        pq.poll();
         
        int val = mini + values[x];
         
        // If on addition it exceeds
        // M then not possible
        if (val > M)
        {
            poss = false;
            break;
        }
        pq.add(val);
    }
     
    // If all elements are added
    if (poss)
    {
        System.out.println("Yes");
    }
    else
    {
        System.out.println("No");
    }
}
 
// Driver Code
public static void main(String args[])
{
    Integer ar[] = { 5, 9, 3, 8, 7 };
    int N = 5;
     
    Integer values[] = { 1, 2, 3, 4 };
    int K = 4;
     
    int M = 9;
     
    solve(ar, values, N, K, M);
}
}
 
// This code is contributed by offbeat


Python3
# Python3 program to implement the
# above approach
from queue import PriorityQueue
 
# Function which checks if all
# additions are possible
def solve(ar, values, N, K, M):
      
    # Sorting values[] in
    # decreasing order
    values.sort(reverse = True)
      
    # Minimum priority queue which
    # contains all the elements
    # of array arr[]
    pq = PriorityQueue()
     
    for x in range(N):
     
        pq.put(ar[x]);
       
    # poss stores whether all the
    # additions are possible
    poss = True;
     
    for x in range(K):
          
        # Minium value in the
        # priority queue
        mini = pq.get();
          
        val = mini + values[x];
          
        # If on addition it exceeds
        # M then not possible
        if (val > M):
          poss = False;
          break;
         
        pq.put(val);
     
    # If all elements are added
    if (poss):
        print("Yes");
    else:
        print("No");
 
# Driver Code
if __name__=='__main__':
 
    ar = [ 5, 9, 3, 8, 7 ]
    N = 5;
      
    values = [ 1, 2, 3, 4 ]
     
    K = 4;
      
    M = 9;
      
    solve(ar, values, N, K, M);
 
# This code is contributed by rutvik_56


C#
// C# Program to implement the
// above approach 
using System;
using System.Collections.Generic;
class GFG
{
     
    // Function which checks if all
    // additions are possible
    static void solve(int[] ar, int[] values,
            int N, int K, int M)
    {
      
        // Sorting values[] in
        // decreasing order
        Array.Sort(values);
        Array.Reverse(values);
      
        // Minimum priority queue which
        // contains all the elements
        // of array arr[]
        List pq = new List();
      
        for (int x = 0; x < N; x++)
        {
            pq.Add(ar[x]);
        }
         
        pq.Sort();
      
        // poss stores whether all the
        // additions are possible
        bool poss = true;
        for (int x = 0; x < K; x++)
        {
      
            // Minium value in the
            // priority queue
            int mini = pq[0];
            pq.RemoveAt(0);
            int val = mini + values[x];
      
            // If on addition it exceeds
            // M then not possible
            if (val > M)
            {
                poss = false;
                break;
            }
            pq.Add(val);
            pq.Sort();
        }
      
        // If all elements are added
        if (poss)
        {
            Console.WriteLine("Yes");
        }
        else
        {
            Console.WriteLine("No");
        }
    }
 
  // Driver code
  static void Main()
  {
    int[] ar = { 5, 9, 3, 8, 7 };
    int N = 5;
  
    int[] values = { 1, 2, 3, 4 };
    int K = 4;
  
    int M = 9;
  
    solve(ar, values, N, K, M);
  }
}
 
// This code is contributed by divyeshrabadiya07.


输出:
Yes

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