Sunday, June 26, 2016
Programming Problems about Stocks
LeetCod 122: Best Time to Buy and Sell Stock II
Say you have an array for which the ith element is the price of a given stock on day i.
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).
class Solution {
public:
int maxProfit(vector<int>& prices) {
if (prices.empty()) return 0;
int max_output = 0;
int max_profit_normal = 0;
int max_profit_sell_at_i = prices.back();
for (int i = prices.size()-2; i >= 0; --i) {
max_profit_normal = max(max_profit_sell_at_i - prices[i], max_profit_normal);
max_profit_sell_at_i = max_profit_normal + prices[i];
}
return max_profit_normal;
}
};
LeetCode 123: Best Time to Buy and Sell Stock III
Say you have an array for which the ith element is the price of a given stock on day i.
Design an algorithm to find the maximum profit. You may complete at most two transactions.
Note:
You may not engage in multiple transactions at the same time (ie, you must sell the stock before you buy again).
class Solution {
public:
int maxProfit(vector<int>& prices) {
vector<int> min_prices(prices.size(), 0);
int min_val = numeric_limits<int>::max();
for (int i = 0; i < prices.size(); ++i) {
min_val = min(min_val, prices[i]);
min_prices[i] = min_val;
}
vector<int> max_prices(prices.size(), 0);
int max_val = numeric_limits<int>::min();
for (int j = prices.size()-1; j >=0; --j) {
max_val = max(max_val, prices[j]);
max_prices[j] = max_val;
}
vector<int> max_profit(prices.size(), 0);
int max_profit_so_far = 0;
for (int i = 0; i < prices.size(); ++i) {
max_profit_so_far = max(max_profit_so_far, prices[i] - min_prices[i]);
max_profit[i] = max_profit_so_far;
}
vector<int> max_profit_from_right(prices.size(), 0);
int max_profit_so_far_from_right = 0;
for (int i = prices.size()-1; i >= 0; --i) {
max_profit_so_far_from_right = max(
max_profit_so_far_from_right, max_prices[i] - prices[i]);
max_profit_from_right[i] = max_profit_so_far_from_right;
}
int output = 0;
for (int i = 0; i < prices.size(); ++i) {
output = max(output, max_profit[i] + max_profit_from_right[i]);
}
return output;
}
};
LeetCode 309: Best Time to Buy and Sell Stock with Cooldown
Say you have an array for which the ith element is the price of a given stock on day i.
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) with the following restrictions:
You may not engage in multiple transactions at the same time (ie, you must sell the stock before you buy again).
After you sell your stock, you cannot buy stock on next day. (ie, cooldown 1 day)
class Solution {
public:
int maxProfit(vector<int>& prices) {
if (prices.empty()) return 0;
int n = prices.size();
vector<int> maxProfit(n, 0);
for (int i = n-2; i >= 0; --i) {
int maxProfit_if_buy_at_i = numeric_limits<int>::min();
for (int j = i+1; j < n; ++j) {
maxProfit_if_buy_at_i = max(maxProfit_if_buy_at_i,
prices[j] - prices[i] + (j + 2 < n ? maxProfit[j + 2] : 0));
}
maxProfit[i] = max(maxProfit[i+1], maxProfit_if_buy_at_i);
}
return maxProfit[0];
}
};
LeetCode 188: Best Time to Buy and Sell Stock IV
Say you have an array for which the ith element is the price of a given stock on day i.
Design an algorithm to find the maximum profit. You may complete at most k transactions.
Note:
You may not engage in multiple transactions at the same time (ie, you must sell the stock before you buy again).
class Solution {
public:
int maxProfit(int k, vector<int>& prices) {
if (prices.empty()) return 0;
int ndays = prices.size();
if (k >= prices.size()/2) {
int res = 0;
for (int i = 1; i < prices.size(); ++i)
res += max(prices[i] - prices[i-1], 0);
return res;
}
vector<int> B(ndays,0);
vector<int> S(ndays,0);
int max_val_B;
int max_val_S;
for (int kk = 0; kk < k; ++kk){
//cout << "===== " << kk << " =====" << endl;
max_val_B = numeric_limits<int>::min();
max_val_S = numeric_limits<int>::min();
for (int i = 0; i < ndays; ++i) {
max_val_B = max(max_val_B, S[i]-prices[i]);
max_val_S = max(max_val_S, max_val_B + prices[i]);
B[i] = max_val_B;
S[i] = max_val_S;
}
}
return S.back();
}
};
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment