Difficulty: 2
Frequency: 1
Problem:
Say you have an array for which the ith element is the price of a given stock on day i.
If you were only permitted to complete at most one transaction (ie, buy one and sell one share of the stock), design an algorithm to find the maximum profit.
Solution:
class Solution {
public:
int maxProfit(vector<int> &prices) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
int i_max_index = 0, i_min_index = 0;
int i_max_profit = 0;
for (int i = 1; i<prices.size(); i++)
{
if (prices[i]<prices[i_min_index])
{
i_min_index = i_max_index = i;
}
else if (prices[i]>=prices[i_max_index])
{
i_max_index = i;
if (i_max_profit<prices[i_max_index] - prices[i_min_index])
i_max_profit = prices[i_max_index] - prices[i_min_index];
}
}
return i_max_profit;
}
};
Note:
I've seen someone solve this problem using DP. Its time complexity is O(nlogn), which is not optimal. My solution's time complexity is O(n);