📜  一个人最多可以吃多少个苹果

📅  最后修改于: 2021-09-07 02:32:51             🧑  作者: Mango

鉴于两个数组苹果[]天[]表示苹果的计数苹果树生产和天数,这些苹果是食用从分别天,任务是找到苹果一个人可以吃,如果最大数量一个人一天最多可以吃一个苹果。

例子

方法:这个想法是吃最接近保质期的苹果。请按照以下步骤解决问题:

  • 初始化priority_queue存储i白天和那些苹果的有效期限生产苹果的计数。
  • 遍历这两个阵列和插入苹果的计数以及由苹果树上i天产生的那些苹果的有效期限。
  • 检查priority_queue 顶部元素的过期日期是否已过期。如果发现为真,则从priority_queue 中弹出元素。
  • 否则,增加最大计数并减少来自priority_queue 的苹果计数。
  • 最后,打印获得的最大计数。

下面是上述方法的实现:

C++
// C++ program of the above approach
#include 
using namespace std;
 
// Function to find the maximum number of apples
// a person can eat such that the person eat
// at most one apple in a day.
int cntMaxApples(vector apples, vector days)
{
 
    // Stores count of apples and number
    // of days those apples are edible
    typedef pair P;
 
    // Store count of apples and number
    // of days those apples are edible
    priority_queue, greater
 

> pq;       // Stores indices of the array     int i = 0;       // Stores count of days     int n = apples.size();       // Stores maximum count of     // edible apples     int total_apples = 0;       // Traverse both the arrays     while (i < n || !pq.empty()) {           // If top element of the apple         // is not already expired         if (i < n && apples[i] != 0) {               // Insert count of apples and             // their expiration date             pq.push({ i + days[i] - 1, apples[i] });         }           // Remove outdated apples         while (!pq.empty() && pq.top().first < i) {             pq.pop();         }           // Insert all the apples produces by         // tree on current day         if (!pq.empty()) {               // Stores top element of pq             auto curr = pq.top();               // Remove top element of pq             pq.pop();               // If count of apples in curr             // is greater than 0             if (curr.second > 1) {                   // Insert count of apples and                 // their expiration date                 pq.push({ curr.first,                           curr.second - 1 });             }               // Update total_apples             ++total_apples;         }           // Update index         ++i;     }       return total_apples; }   // Driver Code int main() {     vector apples = { 1, 2, 3, 5, 2 };     vector days = { 3, 2, 1, 4, 2 };     cout << cntMaxApples(apples, days);     return 0; }


Python3
# Python3 program of the above approach
 
# Function to find the maximum number of apples
# a person can eat such that the person eat
# at most one apple in a day.
def cntMaxApples(apples, days) :
 
    # Store count of apples and number
    # of days those apples are edible
    pq = []
     
    # Stores indices of the array
    i = 0
     
    # Stores count of days
    n = len(apples)
     
    # Stores maximum count of
    # edible apples
    total_apples = 0
     
    # Traverse both the arrays
    while (i < n or len(pq) > 0) :
     
      # If top element of the apple
      # is not already expired
      if (i < n and apples[i] != 0) :
     
        # Insert count of apples and
        # their expiration date
        pq.append([i + days[i] - 1, apples[i]])
        pq.sort()
     
      # Remove outdated apples
      while (len(pq) > 0 and pq[0][0] < i) :
        pq.pop(0)
     
      # Insert all the apples produces by
      # tree on current day
      if (len(pq) > 0) :
     
        # Stores top element of pq
        curr = pq[0]
     
        # Remove top element of pq
        pq.pop(0)
     
        # If count of apples in curr
        # is greater than 0
        if (len(curr) > 1) :
     
          # Insert count of apples and
          # their expiration date
          pq.append([curr[0], curr[1] - 1])
          pq.sort()
     
        # Update total_apples
        total_apples += 1
     
      # Update index
      i += 1
     
    return total_apples
     
apples = [ 1, 2, 3, 5, 2 ]
days = [ 3, 2, 1, 4, 2 ]
 
print(cntMaxApples(apples, days))
 
# This code is contributed by divyesh072019


C#
// C# program of the above approach
using System;
using System.Collections.Generic;
class GFG {
 
  // Function to find the maximum number of apples
  // a person can eat such that the person eat
  // at most one apple in a day.
  static int cntMaxApples(int[] apples, int[] days)
  {
 
    // Store count of apples and number
    // of days those apples are edible
    List> pq = new List>();
 
    // Stores indices of the array
    int i = 0;
 
    // Stores count of days
    int n = apples.Length;
 
    // Stores maximum count of
    // edible apples
    int total_apples = 0;
 
    // Traverse both the arrays
    while (i < n || pq.Count > 0) {
 
      // If top element of the apple
      // is not already expired
      if (i < n && apples[i] != 0) {
 
        // Insert count of apples and
        // their expiration date
        pq.Add(new Tuple(i + days[i] - 1, apples[i]));
        pq.Sort();
      }
 
      // Remove outdated apples
      while (pq.Count > 0 && pq[0].Item1 < i) {
        pq.RemoveAt(0);
      }
 
      // Insert all the apples produces by
      // tree on current day
      if (pq.Count > 0) {
 
        // Stores top element of pq
        Tuple curr = pq[0];
 
        // Remove top element of pq
        pq.RemoveAt(0);
 
        // If count of apples in curr
        // is greater than 0
        if (curr.Item2 > 1) {
 
          // Insert count of apples and
          // their expiration date
          pq.Add(new Tuple(curr.Item1, curr.Item2 - 1));
          pq.Sort();
        }
 
        // Update total_apples
        ++total_apples;
      }
 
      // Update index
      ++i;
    }
 
    return total_apples;
  }   
 
  // Driver code
  static void Main() {
    int[] apples = { 1, 2, 3, 5, 2 };
    int[] days = { 3, 2, 1, 4, 2 };
 
    Console.Write(cntMaxApples(apples, days));
  }
}
 
// This code is contributed by divyeshrabadiya07.


输出:
7

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

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