连续给出n种葡萄酒,其中整数分别表示每种葡萄酒的价格。每年您都可以出售该行中的第一个或最后一个葡萄酒。但是,葡萄酒的价格会随着时间的流逝而上涨。假设从葡萄酒中获得的初始利润为P1,P2,P3…Pn。在第Y年,第ith种葡萄酒的利润将为Y * Pi。对于每一年,您的任务是打印“ beg”或“ end”,以表示应该出售第一种还是最后一种葡萄酒。同样,计算所有葡萄酒的最大利润。
例子 :
输入:葡萄酒价格:2 4 6 2 5输出:乞讨结束乞讨beg乞讨64说明:
方法:这是一个标准的动态编程问题。最初看起来像是一个贪婪的问题,每年我们应该出售便宜的葡萄酒,但是示例案例(第2年)清楚地证明了这种方法是错误的。有时,我们需要提早出售昂贵的葡萄酒,以在以后的几年中节省相对昂贵的葡萄酒(在这里,如果第二年售出4瓶葡萄酒,那么在第四年我们必须卖出2瓶,这将浪费很大的系数)。
第二个问题是“存储策略”以获得计算出的价格,该价格具有相当标准的方法,该方法也可以用于其他问题。这个想法是存储每个状态的最佳动作,并使用该动作从初始状态开始浏览最佳状态。
C++
// Program to calculate maximum price of wines
#include
using namespace std;
#define N 1000
int dp[N][N];
// This array stores the "optimal action"
// for each state i, j
int sell[N][N];
// Function to maximize profit
int maxProfitUtil(int price[], int begin,
int end, int n) {
if (dp[begin][end] != -1)
return dp[begin][end];
int year = n - (end - begin);
if (begin == end)
return year * price[begin];
// x = maximum profit on selling the
// wine from the front this year
int x = price[begin] * year +
maxProfitUtil(price, begin + 1, end, n);
// y = maximum profit on selling the
// wine from the end this year
int y = price[end] * year +
maxProfitUtil(price, begin, end - 1, n);
int ans = max(x, y);
dp[begin][end] = ans;
if (x >= y)
sell[begin][end] = 0;
else
sell[begin][end] = 1;
return ans;
}
// Util Function to calculate maxProfit
int maxProfit(int price[], int n) {
// reseting the dp table
for (int i = 0; i < N; i++)
for (int j = 0; j < N; j++)
dp[i][j] = -1;
int ans = maxProfitUtil(price, 0, n - 1, n);
int i = 0, j = n - 1;
while (i <= j) {
// sell[i][j]=0 implies selling the
// wine from beginning will be more
// profitable in the long run
if (sell[i][j] == 0) {
cout << "beg ";
i++;
} else {
cout << "end ";
j--;
}
}
cout << endl;
return ans;
}
// Driver code
int main() {
// Price array
int price[] = { 2, 4, 6, 2, 5 };
int n = sizeof(price) / sizeof(price[0]);
int ans = maxProfit(price, n);
cout << ans << endl;
return 0;
}
Java
// Program to calculate maximum price of wines
import java.io.*;
class GFG {
static int N = 1000;
static int [][]dp = new int[N][N];
// This array stores the "optimal action"
// for each state i, j
static int [][]sell = new int[N][N];
// Function to maximize profit
static int maxProfitUtil(int price[],
int begin, int end, int n)
{
if (dp[begin][end] != -1)
return dp[begin][end];
int year = n - (end - begin);
if (begin == end)
return year * price[begin];
// x = maximum profit on selling the
// wine from the front this year
int x = price[begin] * year +
maxProfitUtil(price, begin + 1,
end, n);
// y = maximum profit on selling the
// wine from the end this year
int y = price[end] * year +
maxProfitUtil(price, begin,
end - 1, n);
int ans = Math.max(x, y);
dp[begin][end] = ans;
if (x >= y)
sell[begin][end] = 0;
else
sell[begin][end] = 1;
return ans;
}
// Util Function to calculate maxProfit
static int maxProfit(int price[], int n)
{
// reseting the dp table
for (int i = 0; i < N; i++)
for (int j = 0; j < N; j++)
dp[i][j] = -1;
int ans = maxProfitUtil(price, 0,
n - 1, n);
int i = 0, j = n - 1;
while (i <= j) {
// sell[i][j]=0 implies selling
// the wine from beginning will
// be more profitable in the
// long run
if(sell[i][j] == 0){
System.out.print( "beg ");
i++;
}
else
{
System.out.print( "end ");
j--;
}
}
System.out.println();
return ans;
}
// Driver code
public static void main (String[] args)
{
// Price array
int price[] = { 2, 4, 6, 2, 5 };
int n = price.length;
int ans = maxProfit(price, n);
System.out.println( ans );
}
}
// This code is contributed by anuj_67.
Python3
# Python3 Program to calculate
# maximum price of wines
N = 1000
dp = [ [-1 for col in range(N)]
for row in range(N)]
# This array stores the "optimal action"
# for each state i, j
sell = [ [0 for col in range(N)]
for row in range(N)]
# Function to maximize profit
def maxProfitUtil(price, begin, end, n):
if (dp[begin][end] != -1):
return dp[begin][end]
year = n - (end - begin)
if (begin == end):
return year * price[begin]
# x = maximum profit on selling the
# wine from the front this year
x = price[begin] * year + \
maxProfitUtil(price, begin + 1, end, n)
# y = maximum profit on selling the
# wine from the end this year
y = price[end] * year + \
maxProfitUtil(price, begin, end - 1, n)
ans = max(x, y)
dp[begin][end] = ans
if (x >= y):
sell[begin][end] = 0
else:
sell[begin][end] = 1
return ans
# Util Function to calculate maxProfit
def maxProfit(price, n):
ans = maxProfitUtil(price, 0, n - 1, n)
i = 0
j = n - 1
while (i <= j):
# sell[i][j]=0 implies selling the
# wine from beginning will be more
# profitable in the long run
if (sell[i][j] == 0):
print("beg", end = " ")
i = i + 1
else:
print("end", end = " ")
j = j - 1
print(" ")
return ans
# Driver code
# Price array
price = [ 2, 4, 6, 2, 5 ]
size = 5
ans = maxProfit(price, size);
print(ans)
# This code is contributed by ashutosh450
C#
// C# Program to calculate maximum
// price of wines
using System;
class GFG {
static int N = 1000;
static int [,]dp = new int[N, N];
// This array stores the "optimal action"
// for each state i, j
static int [,]sell = new int[N,N];
// Function to maximize profit
static int maxProfitUtil(int []price,
int begin, int end, int n)
{
if (dp[begin,end] != -1)
return dp[begin,end];
int year = n - (end - begin);
if (begin == end)
return year * price[begin];
// x = maximum profit on selling the
// wine from the front this year
int x = price[begin] * year +
maxProfitUtil(price, begin + 1,
end, n);
// y = maximum profit on selling the
// wine from the end this year
int y = price[end] * year +
maxProfitUtil(price, begin,
end - 1, n);
int ans = Math.Max(x, y);
dp[begin,end] = ans;
if (x >= y)
sell[begin,end] = 0;
else
sell[begin,end] = 1;
return ans;
}
// Util Function to calculate maxProfit
static int maxProfit(int []price, int n)
{
int i, j;
// reseting the dp table
for(i = 0; i < N; i++)
for(j = 0; j < N; j++)
dp[i, j] = -1;
int ans = maxProfitUtil(price, 0,
n - 1, n);
i = 0; j = n - 1;
while (i <= j) {
// sell[i][j]=0 implies selling
// the wine from beginning will
// be more profitable in the
// long run
if(sell[i, j] == 0){
Console.Write( "beg ");
i++;
}
else
{
Console.Write( "end ");
j--;
}
}
Console.WriteLine();
return ans;
}
// Driver Code
public static void Main ()
{
// Price array
int []price = {2, 4, 6, 2, 5};
int n = price.Length;
int ans = maxProfit(price, n);
Console.WriteLine( ans );
}
}
// This code is contributed by anuj_67.
输出:
beg end end beg beg
64
时间复杂度: O(n 2 )