给定一个由N 个正整数组成的数组arr[] ,代表N 个孩子的评分,任务是找到分发给N 个孩子所需的最少糖果数量,使得每个孩子至少得到一个糖果,并且评分较高的孩子得到比邻居更多的糖果。
例子:
Input: arr[] = {1, 0, 2}
Output: 5
Explanation:
Consider the distribution of candies as {2, 1, 2} that satisfy the given conditions. Therefore, the sum of candies is 2 + 1 + 2 = 5, which is the minimum required candies.
Input: arr[] = {1, 2, 2}
Output: 4
方法:可以使用贪心方法解决给定的问题。请按照以下步骤解决问题:
- 初始化一个数组,比如ans[]来存储分配给每个孩子的糖果数量,并将其初始化为1到每个数组元素ans[] 。
- 遍历给定的数组arr[]并且如果arr[i + 1]的值大于arr[i]并且ans[i + 1] 的值至少是ans[i] ,则更新ans[的值i + 1]作为ans[i] + 1 。
- 从后面遍历给定数组并执行以下步骤:
- 如果ARR的值[i]是比ARR大于第[i + 1]和ANS [I]的值是至少ANS第[i + 1],然后更新ANS的值[I + 1]如ANS [I ] + 1 。
- 如果ARR [I]的值等于给Arr第[i + 1]和ANS [I]的值小于ANS第[i + 1],然后更新ANS的值[I + 1]如ANS [I ] + 1 。
- 完成上述步骤后,打印数组ans[]的总和作为结果糖果的总和。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to count the minimum number
// of candies that is to be distributed
int countCandies(int arr[], int n)
{
// Stores total count of candies
int sum = 0;
// Stores the amount of candies
// allocated to a student
int ans[n];
// If the value of N is 1
if (n == 1) {
return 1;
}
// Initialize with 1 all array
// element
for (int i = 0; i < n; i++)
ans[i] = 1;
// Traverse the array arr[]
for (int i = 0; i < n - 1; i++) {
// If arr[i+1] is greater than
// arr[i] and ans[i+1] is
// at most ans[i]
if (arr[i + 1] > arr[i]
&& ans[i + 1] <= ans[i]) {
// Assign ans[i]+1 to
// ans[i+1]
ans[i + 1] = ans[i] + 1;
}
}
// Iterate until i is atleast 0
for (int i = n - 2; i >= 0; i--) {
// If arr[i] is greater than
// arr[i+1] and ans[i] is
// at most ans[i+1]
if (arr[i] > arr[i + 1]
&& ans[i] <= ans[i + 1]) {
// Assign ans[i+1]+1 to
// ans[i]
ans[i] = ans[i + 1] + 1;
}
// If arr[i] is equals arr[i+1]
// and ans[i] is less than
// ans[i+1]
else if (arr[i] == arr[i + 1]
&& ans[i] < ans[i + 1]) {
// Assign ans[i+1]+1 to
// ans[i]
ans[i] = ans[i + 1] + 1;
}
// Increment the sum by ans[i]
sum += ans[i];
}
sum += ans[n - 1];
// Return the resultant sum
return sum;
}
// Driver Code
int main()
{
int arr[] = { 1, 0, 2 };
int N = sizeof(arr) / sizeof(arr[0]);
cout << countCandies(arr, N);
return 0;
}
Java
// Java program for the above approach
import java.io.*;
class GFG{
// Function to count the minimum number
// of candies that is to be distributed
static int countCandies(int arr[], int n)
{
// Stores total count of candies
int sum = 0;
// Stores the amount of candies
// allocated to a student
int[] ans = new int[n];
// If the value of N is 1
if (n == 1)
{
return 1;
}
// Initialize with 1 all array
// element
for(int i = 0; i < n; i++)
ans[i] = 1;
// Traverse the array arr[]
for(int i = 0; i < n - 1; i++)
{
// If arr[i+1] is greater than
// arr[i] and ans[i+1] is
// at most ans[i]
if (arr[i + 1] > arr[i] &&
ans[i + 1] <= ans[i])
{
// Assign ans[i]+1 to
// ans[i+1]
ans[i + 1] = ans[i] + 1;
}
}
// Iterate until i is atleast 0
for(int i = n - 2; i >= 0; i--)
{
// If arr[i] is greater than
// arr[i+1] and ans[i] is
// at most ans[i+1]
if (arr[i] > arr[i + 1] &&
ans[i] <= ans[i + 1])
{
// Assign ans[i+1]+1 to
// ans[i]
ans[i] = ans[i + 1] + 1;
}
// If arr[i] is equals arr[i+1]
// and ans[i] is less than
// ans[i+1]
else if (arr[i] == arr[i + 1] &&
ans[i] < ans[i + 1])
{
// Assign ans[i+1]+1 to
// ans[i]
ans[i] = ans[i + 1] + 1;
}
// Increment the sum by ans[i]
sum += ans[i];
}
sum += ans[n - 1];
// Return the resultant sum
return sum;
}
// Driver Code
public static void main(String[] args)
{
int arr[] = { 1, 0, 2 };
int N = arr.length;
System.out.println(countCandies(arr, N));
}
}
// This code is contributed by patel2127
Python3
# Python3 program for the above approach
# Function to count the minimum number
# of candies that is to be distributed
def countCandies(arr, n):
# Stores total count of candies
sum = 0
# Stores the amount of candies
# allocated to a student
ans = [1] * n
# If the value of N is 1
if (n == 1):
return 1
# Initialize with 1 all array
# element
# for (i = 0 i < n i++)
# ans[i] = 1
# Traverse the array arr[]
for i in range(n - 1):
# If arr[i+1] is greater than
# arr[i] and ans[i+1] is
# at most ans[i]
if (arr[i + 1] > arr[i] and
ans[i + 1] <= ans[i]):
# Assign ans[i]+1 to
# ans[i+1]
ans[i + 1] = ans[i] + 1
# Iterate until i is atleast 0
for i in range(n - 2, -1, -1):
# If arr[i] is greater than
# arr[i+1] and ans[i] is
# at most ans[i+1]
if (arr[i] > arr[i + 1] and
ans[i] <= ans[i + 1]):
# Assign ans[i+1]+1 to
# ans[i]
ans[i] = ans[i + 1] + 1
# If arr[i] is equals arr[i+1]
# and ans[i] is less than
# ans[i+1]
elif (arr[i] == arr[i + 1] and
ans[i] < ans[i + 1]):
# Assign ans[i+1]+1 to
# ans[i]
ans[i] = ans[i + 1] + 1
# Increment the sum by ans[i]
sum += ans[i]
sum += ans[n - 1]
# Return the resultant sum
return sum
# Driver Code
if __name__ == '__main__':
arr = [ 1, 0, 2 ]
N = len(arr)
print (countCandies(arr, N))
# This code is contributed by mohit kumar 29
C#
using System;
public class GFG{
// Function to count the minimum number
// of candies that is to be distributed
static int countCandies(int[] arr, int n)
{
// Stores total count of candies
int sum = 0;
// Stores the amount of candies
// allocated to a student
int[] ans = new int[n];
// If the value of N is 1
if (n == 1)
{
return 1;
}
// Initialize with 1 all array
// element
for(int i = 0; i < n; i++)
ans[i] = 1;
// Traverse the array arr[]
for(int i = 0; i < n - 1; i++)
{
// If arr[i+1] is greater than
// arr[i] and ans[i+1] is
// at most ans[i]
if (arr[i + 1] > arr[i] &&
ans[i + 1] <= ans[i])
{
// Assign ans[i]+1 to
// ans[i+1]
ans[i + 1] = ans[i] + 1;
}
}
// Iterate until i is atleast 0
for(int i = n - 2; i >= 0; i--)
{
// If arr[i] is greater than
// arr[i+1] and ans[i] is
// at most ans[i+1]
if (arr[i] > arr[i + 1] &&
ans[i] <= ans[i + 1])
{
// Assign ans[i+1]+1 to
// ans[i]
ans[i] = ans[i + 1] + 1;
}
// If arr[i] is equals arr[i+1]
// and ans[i] is less than
// ans[i+1]
else if (arr[i] == arr[i + 1] &&
ans[i] < ans[i + 1])
{
// Assign ans[i+1]+1 to
// ans[i]
ans[i] = ans[i + 1] + 1;
}
// Increment the sum by ans[i]
sum += ans[i];
}
sum += ans[n - 1];
// Return the resultant sum
return sum;
}
// Driver Code
static public void Main (){
int[] arr = { 1, 0, 2 };
int N = arr.Length;
Console.WriteLine(countCandies(arr, N));
}
}
// This code is contributed by rag2127
Javascript
输出:
5
时间复杂度: O(N)
辅助空间: O(N)
如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程和学生竞争性编程现场课程。