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. // cout<<buy<<" "<<endl;
  16. }
  17. else if(prices[i]-buy>max_profit){
  18. max_profit=prices[i]-buy;
  19. }
  20.  
  21. }
  22. return max_profit;
  23.  
  24. }
  25. };
  26.  
  27. int main() {
  28. // your code goes here
  29. Solution s1;
  30. vector<int> v1={7, 1, 10, 3, 6, 9 };
  31. int maximumprofit=s1.maxProfit(v1);
  32. cout<<maximumprofit<<" ";
  33. return 0;
  34. }
Success #stdin #stdout 0.01s 5280KB
stdin
Standard input is empty
stdout
9