📜  K分钟内可获得的最大钻石数

📅  最后修改于: 2021-09-03 03:59:42             🧑  作者: Mango

给定一个由N 个正整数组成的数组arr[]使得arr[i]表示第i包包含arr[i]钻石和一个正整数K ,任务是找到可以在其中获得的最大钻石数正好K分钟,如果掉落一个袋子需要1分钟,那么如果一个有P颗钻石的袋子掉下来,那么它会变成[P/2]颗钻石,并且获得P颗钻石。

例子:

方法:可以在最大堆的帮助下使用贪心方法解决给定的问题。请按照以下步骤解决问题:

  • 初始化一个优先级队列,比如PQ ,并将给定数组的所有元素插入PQ
  • 初始化一个变量,比如ans0来存储最终获得的最大钻石。
  • 迭代一个循环,直到优先级队列PQ不为空且K > 0的值:
    • 弹出优先级队列的顶部元素并将弹出的元素添加到变量ans
    • 将弹出的元素除以2并将其插入优先级队列PQ
    • K的值减少1
  • 完成以上步骤后,打印ans的值作为结果。

下面是上述方法的实现:

C++
// C++ program for the above approach
#include 
using namespace std;
 
// Function to find the maximum number
// of diamonds that can be gained in
// exactly K minutes
void maxDiamonds(int A[], int N, int K)
{
    // Stores all the array elements
    priority_queue pq;
 
    // Push all the elements to the
    // priority queue
    for (int i = 0; i < N; i++) {
        pq.push(A[i]);
    }
 
    // Stores the required result
    int ans = 0;
 
    // Loop while the queue is not
    // empty and K is positive
    while (!pq.empty() && K--) {
 
        // Store the top element
        // from the pq
        int top = pq.top();
 
        // Pop it from the pq
        pq.pop();
 
        // Add it to the answer
        ans += top;
 
        // Divide it by 2 and push it
        // back to the pq
        top = top / 2;
        pq.push(top);
    }
 
    // Print the answer
    cout << ans;
}
 
// Driver Code
int main()
{
    int A[] = { 2, 1, 7, 4, 2 };
    int K = 3;
    int N = sizeof(A) / sizeof(A[0]);
    maxDiamonds(A, N, K);
 
    return 0;
}


Java
// Java program for the above approach
import java.util.*;
 
class GFG{
 
// Function to find the maximum number
// of diamonds that can be gained in
// exactly K minutes
static void maxDiamonds(int A[], int N, int K)
{
     
    // Stores all the array elements
    PriorityQueue pq = new PriorityQueue<>(
        (a, b) -> b - a);
 
    // Push all the elements to the
    // priority queue
    for(int i = 0; i < N; i++)
    {
        pq.add(A[i]);
    }
 
    // Stores the required result
    int ans = 0;
 
    // Loop while the queue is not
    // empty and K is positive
    while (!pq.isEmpty() && K-- > 0)
    {
         
        // Store the top element
        // from the pq
        int top = pq.peek();
 
        // Pop it from the pq
        pq.remove();
 
        // Add it to the answer
        ans += top;
 
        // Divide it by 2 and push it
        // back to the pq
        top = top / 2;
        pq.add(top);
    }
 
    // Print the answer
    System.out.print(ans);
}
 
// Driver Code
public static void main(String[] args)
{
    int A[] = { 2, 1, 7, 4, 2 };
    int K = 3;
    int N = A.length;
     
    maxDiamonds(A, N, K);
}
}
 
// This code is contributed by 29AjayKumar


Python3
# Python3 program for the above approach
 
# Function to find the maximum number
# of diamonds that can be gained in
# exactly K minutes
def maxDiamonds(A, N, K):
     
    # Stores all the array elements
    pq = []
 
    # Push all the elements to the
    # priority queue
    for i in range(N):
        pq.append(A[i])
         
    pq.sort()
 
    # Stores the required result
    ans = 0
 
    # Loop while the queue is not
    # empty and K is positive
    while (len(pq) > 0 and K > 0):
        pq.sort()
         
        # Store the top element
        # from the pq
        top = pq[len(pq) - 1]
 
        # Pop it from the pq
        pq = pq[0:len(pq) - 1]
 
        # Add it to the answer
        ans += top
 
        # Divide it by 2 and push it
        # back to the pq
        top = top // 2;
        pq.append(top)
        K -= 1
 
    # Print the answer
    print(ans)
 
# Driver Code
if __name__ == '__main__':
     
    A = [ 2, 1, 7, 4, 2 ]
    K = 3
    N = len(A)
     
    maxDiamonds(A, N, K)
 
# This code is contributed by SURENDRA_GANGWAR


输出:
14

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

如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live