forked from ZoranPandovski/al-go-rithms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path2D-DP-Wine.cpp
86 lines (64 loc) · 1.85 KB
/
2D-DP-Wine.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
// Program to calculate maximum price of wines
#include <bits/stdc++.h>
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;
}