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 the current price is higher than the previous day's price,
  13. // we can buy and sell on the same day to make a profit.
  14. if (prices[i] > prices[i - 1]) {
  15. max_profit += prices[i] - prices[i - 1];
  16. }
  17. }
  18.  
  19. return max_profit;
  20. }
  21.  
  22.  
  23. };
  24.  
  25. int main() {
  26. // your code goes here
  27. Solution s1;
  28. vector<int> v1={7,1,5,3,6,4};
  29. int maxProfit1=s1.maxProfit(v1);
  30. cout<<maxProfit1;
  31. return 0;
  32. }
Success #stdin #stdout 0s 5288KB
stdin
Standard input is empty
stdout
7