
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
Maximum profit from sale of wines in C++
Problem statement
Given n wines in a row, with integers denoting the cost of each wine respectively. Each year you can sale the first or the last wine in the row. The price of wines increases over time. Let the initial profits from the wines be P1, P2, P3…Pn. On the Yth year, the profit from the ith wine will be Y*Pi. For each year, your task is to print start or end denoting whether first or last wine should be sold. Also, calculate the maximum profit from all the wines.
Example
If wine prices are {2, 4, 6, 2, 5} then output will be: start end end start start Maximum profit = 64
Algorithm
We can use dynamic programming to solve this problem −
- The idea is to store the optimal action for each state and use that to navigate through the optimal states starting from the initial states
Example
#include <bits/stdc++.h> using namespace std; #define N 1000 int dp[N][N]; int sell[N][N]; 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]; } int x = price[begin] * year + maxProfitUtil(price, begin + 1, end, n); 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; } int maxProfit(int price[], int n) { 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) { if (sell[i][j] == 0) { cout << "start "; i++; } else { cout << "end "; j--; } } cout << endl; return ans; } int main() { int price[] = { 2, 4, 6, 2, 5 }; int n = sizeof(price) / sizeof(price[0]); int ans = maxProfit(price, n); cout << "Maximum profit = " << ans << endl; return 0; }
Output
When you compile and execute above program. It generates following output −
start end end start start Maximum profit = 64
- Related Articles
- Maximum Profit in Job Scheduling in C++
- Maximum profit after buying and selling the stocks in C++
- C++ code to get maximum profit by house making
- C++ Program to find out the maximum amount of profit that can be achieved from selling wheat
- Program to find maximum profit by cutting the rod of different length in C++
- Program to find maximum profit we can make by holding and selling profit in Python
- Program to get maximum profit by scheduling jobs in Python
- Find Selling Price from given Profit Percentage and Cost in C++
- Maximum GCD from Given Product of Unknowns in C++
- C++ program to find maximum profit we can make by making hamburger and chicken burgers
- Maximum Perimeter Triangle from array in C++
- Most Profit Assigning Work in C++
- Program to find maximum profit by selling diminishing-valued colored balls in Python
- Maximum profit by buying and selling a share at most twice
- Applications of Electronic Point of Sale (EPOS)

Advertisements