forked from Hrudhay-H/Cpp_Algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLeetcode_121.cpp
More file actions
34 lines (27 loc) · 1.08 KB
/
Copy pathLeetcode_121.cpp
File metadata and controls
34 lines (27 loc) · 1.08 KB
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
#include<iostream>
#include<vector>
using namespace std;
// Function to calculate the maximum profit from stock prices
int maxProfit(vector<int>& prices) {
int maxProfit = 0; // Initialize the maximum profit to 0
int bestBuy = prices[0]; // Assume the first day's price is the best buying price initially
// Traverse the prices from the second day onward
for(int i = 1; i < prices.size(); i++) {
// If selling on the current day gives a profit, update maxProfit
if(prices[i] > bestBuy) {
maxProfit = max(maxProfit, prices[i] - bestBuy); // Calculate profit and update maxProfit
}
// Update the best buying price if a lower price is found
bestBuy = min(bestBuy, prices[i]);
}
return maxProfit; // Return the maximum profit
}
int main() {
// Vector representing stock prices on different days
vector<int> prices = {7, 1, 5, 3, 6, 4};
// Call the function to calculate maximum profit
int ans = maxProfit(prices);
// Display the result
cout << "The max profit is " << ans;
return 0; // End of program
}