-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path#47.cpp
29 lines (24 loc) · 1.01 KB
/
#47.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
/*
This problem was asked by Facebook.
Given a array of numbers representing the stock prices of a company in chronological order,
write a function that calculates the maximum profit you could have made from buying and selling that stock "once".
You must buy before you can sell it.
For example, given [9, 11, 8, 5, 7, 10], you should return 5, since you could buy the stock at 5 dollars and sell it at 10 dollars.
*/
#include<bits/stdc++.h>
using namespace std;
// Logic: Think locally, not globally i.e., in the above example, consider the *ascending series's* that is,
// [9, 11] buy = 9, sell = 11 => profit = 2
// [8] buy = 9, sell = 8 => profit = 0
// [5, 7, 10] buy = 5, sell = 10 => profit = 5
// Note: Above variable "sell" is represented by nums[i]
int maxProfit(vector<int> nums){
int n = nums.size();
int buy = nums[0];
int max_profit = 0;
for(int i = 1; i < n; i++){
if(nums[i] > buy) max_profit = max(max_profit, nums[i] - buy);
else buy = nums[i];
}
return max_profit;
}