学会思考
刻意练习
/*
 * Best Time to Buy and Sell Stock I
 * Say you have an array for which the ith element is the price of a given stock on day i.
 * If you were only permitted to complete at most one transaction (ie,
 * buy one and sell one share of the stock), design an algorithm to find the maximum profit.
*/
//references:https://segmentfault.com/a/1190000003483697


class Solution{
    int maxProfit(vector<int>& prices){
        int max = 0, begin = 0,end = 0,delta = 0;
        for(int i = 0; i < prices.size(); i++){
            end = i;
            delta = prices[end] - prices[begin];
            if(delta <= 0){
                begin = i;
            }

            if(delta > max){
                max = delta;
            }
        }
        return max;
    }
};

  

posted on 2020-03-06 16:00  Worty  阅读(124)  评论(0编辑  收藏  举报