fork download
  1. #include <iostream>
  2. #include<vector>
  3.  
  4. using namespace std;
  5. class Solution {
  6. public:
  7. int maxProfit(vector<int>& prices) {
  8. int buy=prices[0];
  9. int max_profit=0;
  10. // Run the loop for the given equation
  11. for(int i=1;i<prices.size();i++){
  12. if(buy>prices[i]){
  13. buy=prices[i];
  14. // cout<<buy<<" "<<endl;
  15. }
  16. else if(prices[i]-buy>max_profit){
  17. max_profit=prices[i]-buy;
  18. }
  19.  
  20. }
  21. return max_profit;
  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 maxProfit1=s1.maxProfit(v1);
  31. cout<<maxProfit1;
  32. return 0;
  33. }
Success #stdin #stdout 0.01s 5284KB
stdin
Standard input is empty
stdout
5