Say you have an array for which the ith element is the price of a given stock on dayi.
Design an algorithm to find the maximum profit. You may complete as many transactions as you like (ie, buy one and sell one share of the stock multiple times). However, you may not engage in multiple transactions at the same time (ie, you must sell the stock before you buy again).
An example will make everything clear!
Suppose we have stock prices as follows:
prices : [1, 2, 3, 1, 2]:
Since I can take as many transactions as I like. A clear buyer will first buy prices[0], waiting till prices[2], sell the stocks, she can earn 2. Then buy at prices[3] and sell at prices[4]. The main rule here is to buy at lowest price and wait till the top sharp price to sell. But, she need to pay attention to such a condition...
#include <vector>
#include <iostream>
using namespace std;
int maxProfit(vector<int>& prices) {
if(prices.size() <= 1) return 0;
int profits = 0;
int maxProfit = 0;
int minPrice = prices[0];
int i = 1, j = 0;
while(i < prices.size()) {
if(prices[i] > prices[j]) {
; // since the stocks are in increasing trend, only idiot will sell...
} else {
profits += (prices[j] - minPrice); // it falls down, sell it!
maxProfit = max(maxProfit, profits);
minPrice = prices[i];
}
i++;
j++;
}
if(prices[i-1] > minPrice) maxProfit += prices[i-1] - minPrice; // pay attention to the last one.
return maxProfit;
}
int main(void) {
vector<int> prices{1, 5, 3, 1, 2, 3};
int profits = maxProfit(prices);
cout << profits << endl;
}
Output Transcation:
#include "header.h"
using namespace std;
// buy and sell many times.
// requirement: not only output the maxProfit, but also the transactions.
vector< pair<int, int> > maxProfit(vector<int>& prices, int& maxProfit) {
int profit = 0;
int i = 1, j = 0, minPriceIndex = 0;
vector< pair<int, int> > transactions;
while(i < prices.size()) {
if(prices[i] >= prices[j]) {;}
else {
profit += prices[j] - prices[minPriceIndex];
if(j - minPriceIndex >= 1)
transactions.push_back(make_pair(minPriceIndex, j));
maxProfit = max(maxProfit, profit);
minPriceIndex = i;
}
i++;
j++;
}
if(prices[i-1] > prices[minPriceIndex]) {
maxProfit += prices[i-1] - prices[minPriceIndex];
transactions.push_back(make_pair(minPriceIndex, i - 1));
}
return transactions;
}
int main(void) {
vector<int> prices{1, 5, 3, 1, 2, 3};
int profit = 0;
vector< pair<int, int> > transactions = maxProfit(prices, profit);
for(int i = 0; i < transactions.size(); ++i) {
cout << transactions[i].first << " " << transactions[i].second << endl;
}
cout << profit << endl;
}
Updated Version, easier to understand
// Buy and Sell as many time as you want.
int maxProfit(vector<int>& prices) {
if(prices.size() <= 1) return 0;
int small_price_so_far = prices[0];
int profit_so_far = 0;
int i = 1;
while(i < prices.size()) {
if(prices[i] > small_price_so_far) {
int profit = prices[i] - small_price_so_far;
profit_so_far ++ profit;
small_price_so_far = prices[i];
} else { // prices[i] < small_price_so_far, price goes down, buy the stock when reaches the bottom.
small_price_so_ar = prices[i];
}
i++;
}
if(prices[i-1] > small_price_so_far) profit_so_far += (prices[i-1] - small_price_so_far);
return profit_so_far;
}
Facebook asked a interesting one:
/*
Best time to sell stock II. You can sell the stock as many times as you want.
Everytime, one sell will cost 3 dollors. How to make the max profit.
*/
#include <vector>
#include <iostream>
using namespace std;
int maxProfit(vector<int>& prices) {
if(prices.size() <= 1) return 0;
int i = 1, j = 0;
int maxProfit = 0;
int minPrice = prices[0];
int profits = 0;
while(i < prices.size()) {
if(prices[i] > prices[j]) {;}
else {
profits += (prices[j] - minPrice) - 3;
maxProfit = max(maxProfit, profits);
minPrice = prices[i];
}
i++;
j++;
}
if(prices[i - 1] > minPrice) {
profits += (prices[i-1] - minPrice) - 3;
maxProfit = max(maxProfit, profits);
}
return maxProfit;
}
int main(void) {
vector<int> prices{1, 5, 3, 1, 2, 3};
cout << maxProfit(prices) << endl;
}
Another interesting variation!
Say, we can only buy one stock, sell all the bought stocks or not buying not selling each day. Calculate the maximum profit can made.
Example : [0, 2, 10, 3, 4] --> 10 + 8 + 1 = 19, we can buy at 0 and 2, sell it at 10. then buy at 3, sell at 4.
The key point to solve this problem to find the maxValue index and recursively calculating.
#include "header.h"
using namespace std;
void maxProfit(vector<int>& array, int left, int right, int& maxValue) {
if(left >= right) return;
int max = 0, pos = 0;
for(int i = left; i <= right; ++i) {
if(max <= array[i]) {
max = array[i];
pos = i;
}
}
for(int i = left; i <= pos; ++i) {
maxValue += array[pos] - array[i];
}
maxProfit(array, pos + 1, right, maxValue);
}
int maxProfit(vector<int>& array) {
int maxValue = 0;
int left = 0, right = array.size() - 1;
maxProfit(array, left, right, maxValue);
return maxValue;
}
int main(void) {
vector<int> array {1, 10, 4, 5, 8};
cout << maxProfit(array) << endl;
}