📜  销售葡萄酒的最大利润

📅  最后修改于: 2021-09-17 07:47:38             🧑  作者: Mango

给定连续 n 种葡萄酒,整数分别表示每种葡萄酒的成本。每年,您都可以出售该行中的第一款或最后一款葡萄酒。然而,葡萄酒的价格会随着时间的推移而上涨。让葡萄酒的初始利润为 P1、P2、P3…Pn。在第 Y 年,第 i 款酒的利润为 Y*Pi。对于每一年,您的任务是打印“beg”或“end”,表示应该出售第一款酒还是最后一款酒。此外,计算所有葡萄酒的最大利润。
例子 :

Input: Price of wines: 2 4 6 2 5
Output: beg end end beg beg 
         64
Explanation :

方法:这是一个标准的动态规划问题。它最初看起来像是一个贪婪的问题,我们应该每年出售更便宜的葡萄酒,但示例案例(第 2 年)清楚地证明这种方法是错误的。有时我们需要提前卖掉昂贵的酒,以便为以后几年保存相对昂贵的酒(这里,如果第2年卖4个,第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.


Javascript


输出:
beg end end beg beg 
64

时间复杂度: O(n 2 )

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程