📜  治愈N人所需的最少天数

📅  最后修改于: 2021-05-17 04:41:53             🧑  作者: Mango

给定一个数组arr [] ,它代表医院中N人的年龄,并且只有一位医生每天可以给最多P个人提供免疫剂量,任务是找到使该人接受免疫治疗所需的最少天数可以使高危人群和正常人群在同一天不能服药。
注意:任何年龄≤10≥60的人都被视为高风险人

例子:

方法:请按照以下步骤解决问题:

  1. 初始化一个变量,例如risk ,以存储年龄小于或等于10且大于或等于60的人数。
  2. 初始化一个变量normal_risk ,以存储年龄在[11,59]范围内的人数
  3. 遍历数组,并检查年龄是否小于或等于10或大于或等于60 。如果发现是真的,则增加风险值。
  4. 否则,增加normal_risk的值。
  5. 最后,打印ceil(risk / P)+ ceil(normal_risk / P)的值

下面是上述方法的实现:

C++
// C++ Program for the above approach
 
#include 
using namespace std;
 
// Function to find minimum count of days required
// to give a cure such that the high risk person
// and risk person does not get a dose on same day.
void daysToCure(int arr[], int N, int P)
{
 
    // Stores count of persons whose age is
    // less than or equal to 10 and
    // greater than or equal to 60.
    int risk = 0;
 
    // Stores the count of persons
    // whose age is in the range [11, 59]
    int normal_risk = 0;
 
    // Traverse the array arr[]
    for (int i = 0; i < N; i++) {
 
        // If age less than or equal to 10
        // or greater than or equal to 60
        if (arr[i] >= 60 || arr[i] <= 10) {
 
            // Update risk
            risk++;
        }
        else {
 
            // Update normal_risk
            normal_risk++;
        }
    }
 
    // Calculate days to cure risk
    // and normal_risk persons
    int days = (risk / P) + (risk % P > 0)
               + (normal_risk / P)
               + (normal_risk % P > 0);
 
    // Print the days
    cout << days;
}
 
// Driver Code
int main()
{
    // Given array
    int arr[] = { 9, 80, 27, 72, 79 };
 
    // Size of the array
    int N = sizeof(arr) / sizeof(arr[0]);
 
    // Given P
    int P = 2;
 
    daysToCure(arr, N, P);
 
    return 0;
}


Java
// Java Program for the above approach
class GFG
{
  // Function to find minimum count of days required
  // to give a cure such that the high risk person
  // and risk person does not get a dose on same day.
  static void daysToCure(int arr[], int N, int P)
  {
  
    // Stores count of persons whose age is
    // less than or equal to 10 and
    // greater than or equal to 60.
    int risk = 0;
  
    // Stores the count of persons
    // whose age is in the range [11, 59]
    int normal_risk = 0;
  
    // Traverse the array arr[]
    for (int i = 0; i < N; i++)
    {
  
      // If age less than or equal to 10
      // or greater than or equal to 60
      if (arr[i] >= 60 || arr[i] <= 10)
      {
  
        // Update risk
        risk++;
      }
      else
      {
  
        // Update normal_risk
        normal_risk++;
      }
    }
  
    // Calculate days to cure risk
    // and normal_risk persons
    int days = (risk / P) +  (normal_risk / P);
  
    if(risk % P > 0)
    {
      days++;
    }
  
    if(normal_risk % P > 0)
    {
      days++;
    }
  
    // Print the days
    System.out.print(days);
  } 
 
    public static void main(String[] args) {
        // Given array
        int arr[] = { 9, 80, 27, 72, 79 };
      
        // Size of the array
        int N = arr.length;
      
        // Given P
        int P = 2;  
        daysToCure(arr, N, P);
    }
}
 
// This code is contributed by divyeshrabadiya07


Python3
# Python3 Program for the above approach
 
# Function to find minimum count of days required
# to give a cure such that the high risk person
# and risk person does not get a dose on same day.
def daysToCure(arr, N, P):
 
    # Stores count of persons whose age is
    # less than or equal to 10 and
    # greater than or equal to 60.
    risk = 0
 
    # Stores the count of persons
    # whose age is in the range [11, 59]
    normal_risk = 0
 
    # Traverse the array arr[]
    for i in range(N):
 
        # If age less than or equal to 10
        # or greater than or equal to 60
        if (arr[i] >= 60 or arr[i] <= 10):
 
            # Update risk
            risk += 1
        else:
 
            # Update normal_risk
            normal_risk += 1
 
    # Calculate days to cure risk
    # and normal_risk persons
    days = (risk // P) + (risk % P > 0) + (normal_risk // P) + (normal_risk % P > 0)
 
    # Prthe days
    print (days)
 
# Driver Code
if __name__ == '__main__':
   
    # Given array
    arr = [9, 80, 27, 72, 79 ]
 
    # Size of the array
    N = len(arr)
 
    # Given P
    P = 2
 
    daysToCure(arr, N, P)
 
    # This code is contributed by mohit kumar 29.


C#
// C# Program for the above approach
using System;
class GFG
{
 
  // Function to find minimum count of days required
  // to give a cure such that the high risk person
  // and risk person does not get a dose on same day.
  static void daysToCure(int[] arr, int N, int P)
  {
 
    // Stores count of persons whose age is
    // less than or equal to 10 and
    // greater than or equal to 60.
    int risk = 0;
 
    // Stores the count of persons
    // whose age is in the range [11, 59]
    int normal_risk = 0;
 
    // Traverse the array arr[]
    for (int i = 0; i < N; i++)
    {
 
      // If age less than or equal to 10
      // or greater than or equal to 60
      if (arr[i] >= 60 || arr[i] <= 10)
      {
 
        // Update risk
        risk++;
      }
      else
      {
 
        // Update normal_risk
        normal_risk++;
      }
    }
 
    // Calculate days to cure risk
    // and normal_risk persons
    int days = (risk / P) +  (normal_risk / P);
 
    if(risk % P > 0)
    {
      days++;
    }
 
    if(normal_risk % P > 0)
    {
      days++;
    }
 
    // Print the days
    Console.Write(days);
  }
 
  // Driver code
  static void Main()
  {
 
    // Given array
    int[] arr = { 9, 80, 27, 72, 79 };
 
    // Size of the array
    int N = arr.Length;
 
    // Given P
    int P = 2;  
    daysToCure(arr, N, P);
  }
}
 
// This code is contributed by divyesh072019.


Javascript


输出
3

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