Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions Coding/Best Time to Buy and Sell Stock.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#include <iostream>
#include <vector>
#include <algorithm>

class Solution {
public:
int maxProfit(std::vector<int>& prices) {
int maxProfit = 0;
int mini = prices[0];

for(int i = 1; i < prices.size(); i++) {
int curProfit = prices[i] - mini;
maxProfit = std::max(maxProfit, curProfit);
mini = std::min(mini, prices[i]);
}

return maxProfit;
}
};

int main() {
// Example usage of the Solution class
std::vector<int> prices = {7, 1, 5, 3, 6, 4};
Solution solution;
int maxProfit = solution.maxProfit(prices);

std::cout << "Maximum profit: " << maxProfit << std::endl;

return 0;
}