![Trending Articles on Technical and Non Technical topics](/images/trending_categories.jpeg)
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Best Time to Buy and Sell Stock IV in C++
Suppose we have an array for which the i-th element is the price of a given stock for the day i. We have to devise an algorithm to find the maximum profit. We can complete at most k transactions. So if the input is like [3,2,6,4,0,3] and k = 2, then the output will be 7, as buy on day 2 (when price = 2) and sell on day 3 (when price = 6), profit will be 6-2 = 4. Then buy on day 5 (price is 0) and sell on day 6 (price is 3), profit will be 3-0 = 3.
To solve this, we will follow these steps −
Define one 3D array of order N + 5 x N + 5 x 2
Define one method called pre()
for initializing i := 0, when i<=N, increase i by 1 do −
for initializing j := 0, when j<=N, increase j by 1 do −
dp[i, j, 1] := - 1, dp[i, j, 0] := - 1
Define one method called solve(), this will take arr, i, n, k and status
if i is same as n, then,
if status is non-zero, then,
return - 100000
return 0
if dp[i, k, status] is not equal to -1, then,
return dp[i, k, status]
ans := solve(arr,i + 1,n,k,status)
if status is non-zero, then,
ans := max of ans, solve(arr,i + 1,n,k - 1, inverse of status) + arr[i]
Otherwise −
if k>0, then,
ans := max of ans,solve(arr,i + 1,n,k,inverse of status status) - arr[i]
return dp[i, k, status] := ans
From the main method do the following −
Call the function pre()
if k >= size of prices /2, then,
ans := 0
for initializing i := 1, when i < size of prices, increase i by 1 do −
if prices[i] > prices[i-1], then, ans = ans + prices[i] - prices[i - 1]
return ans
return solve(prices,0, size of prices, k, 0)
Example
Let us see the following implementation to get better understanding −
#include <bits/stdc++.h> using namespace std; typedef int lli; const lli N = 1000; lli dp[N + 5][N + 5][2]; class Solution { public: void pre(){ for(lli i =0;i<=N;i++){ for(lli j = 0;j<=N;j++){ dp[i][j][1]=-1; dp[i][j][0]=-1; } } } lli solve(vector<int> &arr, lli i,lli n,lli k, lli status){ if(i == n){ if(status)return -100000; return 0; } if(dp[i][k][status]!=-1)return dp[i][k][status]; lli ans = solve(arr, i+1,n,k,status); if(status){ ans = max(ans,solve(arr,i+1,n,k-1,!status)+ arr[i]) ; } else { if(k>0){ ans = max(ans,(lli)solve(arr,i+1,n,k,!status)- arr[i]) ; } } return dp[i][k][status] = ans; } int maxProfit(int k, vector<int>& prices) { pre(); if(k>=prices.size()/2){ int ans = 0; for(int i = 1; i < prices.size(); i++){ if(prices[i] > prices[i-1])ans += prices[i] - prices[i-1]; } return ans; } return solve(prices,0,prices.size(),k,0); } }; main(){ Solution ob; vector<int> v = {3,2,6,4,0,3}; cout << (ob.maxProfit(2, v)); }
Input
{ 3,2,6,4,0,3}
Output
7