Simple C++ 6ms solution


  • 0
     int maxProfit(vector<int>& prices) {
            if(prices.size()==0) return 0;
            int profit=0;
            vector<int>::iterator cur=prices.begin();
            vector<int>::iterator next=cur+1;
            while(next!=prices.end()){
                if(*next>*cur) profit+=*next-*cur;
                cur++;
                next++;
            }
            return profit;
        }
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.