给定一个大小为n的数组arr[] ,其中arr[i]是i类型的糖果数量。你有无限量的钱。任务是尽可能多地购买满足以下条件的糖果:
如果您购买x(i)种i型糖果(显然,0 ≤ x(i) ≤ arr[i]),那么对于所有j (1 ≤ j ≤ i),至少必须满足以下一项:
- x(j) < x(i) (你买的 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
方法:我们可以使用贪心方法,从数组的末尾开始。如果我们吃了x 个i + 1类型的糖果,那么我们只能吃min(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.
?>
Javascript
输出:
10