Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- /*
- QUESTION - https://leetcode.com/problems/best-time-to-buy-and-sell-stock/
- */
- int getMaxProfitOneTransaction(vector < int > & prices) {
- int min = -1, maxProfit = 0;
- for (int i = 0; i < prices.size(); i++) {
- //set a minimum to buy on that day
- if (min == -1 || prices[min] > prices[i]) {
- min = i;
- }
- //repeatitvely capture the max profit you can make with the current minimum
- else {
- maxProfit = max(maxProfit, prices[i] - prices[min]);
- }
- }
- return maxProfit;
- }
- int main() {
- // your code goes here
- int n;
- cin >> n;
- vector < int > prices(n);
- for (int i = 0; i < n; i++) {
- cin >> prices[i];
- }
- cout << getMaxProfitOneTransaction(prices) << '\n';
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement