/
best_time_to_buy_sell.cpp
54 lines (49 loc) · 1.45 KB
/
best_time_to_buy_sell.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
/*
* You are provided a vector of numbers, where each number represents
* price of a stock on ith day. If you are permitted to only complete
* one transaction per day (i.e buy one and sell one stock), design
* an algorithm to find the maximum profit.
*
* Input: [7, 1, 5, 3, 6, 4]
* Output: 5
* max. difference = 6-1 = 5
* (not 7-1 = 6, as selling price needs to be larger than buying price)
*
* Approach:
*
* We need to find the maximum difference between two numbers in the array
* (which would be maximum profit) such that selling price(second number)
* is bigger than buying price.
*/
#include <iostream>
#include <vector>
#include <limits>
int maximum_profit(const std::vector<int>& prices)
{
int min_price = std::numeric_limits<int>::max();
int max_profit = 0;
for (int i = 0; i < prices.size(); ++i) {
if (prices[i] < min_price) {
min_price = prices[i];
} else if (prices[i] - min_price > max_profit) {
max_profit = prices[i] - min_price;
}
}
return max_profit;
}
void print_vector(const std::vector<int>& vec) {
for (auto v : vec) {
std::cout << v << " ";
}
std::cout << std::endl;
}
int main()
{
std::vector<int> prices{7, 1, 5, 3, 6, 4};
std::cout << "Prices of stocks in last " << prices.size()
<< " days:" << std::endl;
print_vector(prices);
std::cout << "Maximum profit: " << maximum_profit(prices)
<< std::endl;
return 0;
}