// Best Time to Buy and Sell Stock 1
class Solution { public: int maxProfit(vector<int>& prices) { if(prices.size()==0) return 0; int maxres=prices[prices.size()-1]; int ans=0; for(int i=prices.size()-1;i>=0;i--) { maxres=max(maxres,prices[i]); ans=max(ans,maxres-prices[i]); } return ans; } };
// Best Time to Buy and Sell Stock 2 class Solution { public: int maxProfit(vector<int>& prices) { int n=prices.size(); res=0; for(int i=0;i<n-1;i++) { if(prices[i]<prices[i+1]) res+=prices[i+1]-prices[i]; } return res; };