给定一个数组arr[] ,代表医院中N人的年龄,并且只有一名医生每天最多可以给P人免疫剂量,任务是找到给予免疫所需的最少天数治愈,使得高危人群和正常人不会在同一天服用一剂。
注意:任何年龄≤ 10岁或≥ 60岁的人都被视为高危人群。
例子:
Input: arr[] = {9, 80, 27, 72, 79}, P = 2
Output: 3
Explanation:
There are 4 high risk persons. Therefore, on days 1 and 2, 2 high risk persons can be immuned. On the last day, the normal person can be immuned. Therefore, 3 days are required.
处理方法:按照以下步骤解决问题:
- 初始化一个变量,比如risk ,以存储年龄小于或等于10且大于或等于60 的人数。
- 初始化一个变量,比如normal_risk ,以存储年龄在[11, 59]范围内的人数
- 遍历数组并检查年龄是否小于等于10或大于等于60 。如果发现为真,则增加risk的值。
- 否则,增加normal_risk的值。
- 最后,打印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)
如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live