给定大小为n的数组arr [] ,其中arr [i]是i型糖果的数量。你有无限量的钱。任务是购买满足以下条件的尽可能多的糖果:
如果您购买类型的X(I)糖果我(显然,0≤X(I)≤改编[1]),那么对于所有j(1≤Ĵ≤I)中的必须持有至少一项:
- x(j)
(购买的j型糖果比i型糖果少) - x(j)= 0 (您购买了0个j型糖果)
例子:
Input:arr[] = {1, 2, 1, 3, 6}
Output: 10
x[] = {0, 0, 1, 3, 6} where x[i] is the number of candies bought of type i
Input: arr[] = {3, 2, 5, 4, 10}
Output: 20
Input: arr[] = {1, 1, 1, 1}
Output: 1
方法:我们可以使用贪婪方法,并从数组末尾开始。如果我们采取了类型i + 1的X糖果那么我们只能采取分钟(ARR [I]中,x – 1)I型的糖果。如果该值为负,则我们无法购买当前类型的糖果。
下面是上述方法的实现:
C++
// C++ implementation of the approach
#include
using namespace std;
// Function to return the maximum candies
// that can be bought
int maxCandies(int arr[], int n)
{
// Buy all the candies of the last type
int prevBought = arr[n - 1];
int candies = prevBought;
// Starting from second last
for (int i = n - 2; i >= 0; i--) {
// Amount of candies of the current
// type that can be bought
int x = min(prevBought - 1, arr[i]);
if (x >= 0) {
// Add candies of current type
// that can be bought
candies += x;
// Update the previous bought amount
prevBought = x;
}
}
return candies;
}
// Driver code
int main()
{
int arr[] = { 1, 2, 1, 3, 6 };
int n = sizeof(arr) / sizeof(arr[0]);
cout << maxCandies(arr, n);
return 0;
}
Java
// Java implementation of the approach
class GFG
{
// Function to return the maximum candies
// that can be bought
static int maxCandies(int arr[], int n)
{
// Buy all the candies of the last type
int prevBought = arr[n - 1];
int candies = prevBought;
// Starting from second last
for (int i = n - 2; i >= 0; i--)
{
// Amount of candies of the current
// type that can be bought
int x = Math.min(prevBought - 1, arr[i]);
if (x >= 0)
{
// Add candies of current type
// that can be bought
candies += x;
// Update the previous bought amount
prevBought = x;
}
}
return candies;
}
// Driver code
public static void main(String[] args)
{
int arr[] = { 1, 2, 1, 3, 6 };
int n = arr.length;
System.out.println(maxCandies(arr, n));
}
}
// This code is contributed by Code_Mech.
Python3
# Python3 implementation of the approach
# Function to return the maximum candies
# that can be bought
def maxCandies(arr, n) :
# Buy all the candies of the last type
prevBought = arr[n - 1];
candies = prevBought;
# Starting from second last
for i in range(n - 2, -1, -1) :
# Amount of candies of the current
# type that can be bought
x = min(prevBought - 1, arr[i]);
if (x >= 0) :
# Add candies of current type
# that can be bought
candies += x;
# Update the previous bought amount
prevBought = x;
return candies;
# Driver code
if __name__ == "__main__" :
arr = [ 1, 2, 1, 3, 6 ];
n = len(arr)
print(maxCandies(arr, n));
# This code is contributed by Ryuga
C#
// C# implementation of the approach
using System;
class GFG
{
// Function to return the maximum candies
// that can be bought
static int maxCandies(int[] arr, int n)
{
// Buy all the candies of the last type
int prevBought = arr[n - 1];
int candies = prevBought;
// Starting from second last
for (int i = n - 2; i >= 0; i--)
{
// Amount of candies of the current
// type that can be bought
int x = Math.Min(prevBought - 1, arr[i]);
if (x >= 0)
{
// Add candies of current type
// that can be bought
candies += x;
// Update the previous bought amount
prevBought = x;
}
}
return candies;
}
// Driver code
public static void Main()
{
int[] arr= { 1, 2, 1, 3, 6 };
int n = arr.Length;
Console.WriteLine(maxCandies(arr, n));
}
}
// This code is contributed by Code_Mech.
PHP
= 0; $i--)
{
// Amount of candies of the current
// type that can be bought
$x = min($prevBought - 1, $arr[$i]);
if ($x >= 0)
{
// Add candies of current type
// that can be bought
$candies += $x;
// Update the previous bought amount
$prevBought = $x;
}
}
return $candies;
}
// Driver code
$arr = array(1, 2, 1, 3, 6 );
$n = sizeof($arr);
echo(maxCandies($arr, $n));
// This code is contributed by Code_Mech.
?>
输出:
10
如果您希望与行业专家一起参加现场课程,请参阅《 Geeks现场课程》和《 Geeks现场课程美国》。