fork download
  1. #include <iostream>
  2. #include<vector>
  3.  
  4. using namespace std;
  5. // To find the best condition for the Buy and Sell stock
  6. class Solution {
  7. public:
  8. int maxProfit(vector<int>& prices) {
  9. int buy=prices[0];
  10. int max_profit=0;
  11. // Run the loop for the given equation
  12. for(int i=1;i<prices.size();i++){
  13. if(buy>prices[i]){
  14. buy=prices[i];
  15. }
  16. else if(prices[i]-buy>max_profit){
  17. max_profit=prices[i]-buy;
  18. }
  19. return max_profit;
  20.  
  21. }
  22.  
  23. }
  24. };
  25.  
  26. int main() {
  27. // your code goes here
  28. Solution s1;
  29. vector<int> v1={7, 1, 5, 3, 6, 4 };
  30. int maximumprofit=s1.maxProfit(v1);
  31. cout<<maximumprofit<<" ";
  32. return 0;
  33. }
Success #stdin #stdout 0.01s 5300KB
stdin
Standard input is empty
stdout
0