zoukankan      html  css  js  c++  java
  • 121. Best Time to Buy and Sell Stock

    Problem statement:

    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.

    Example 1:

    Input: [7, 1, 5, 3, 6, 4]
    Output: 5
    
    max. difference = 6-1 = 5 (not 7-1 = 6, as selling price needs to be larger than buying price)

    Example 2:

    Input: [7, 6, 4, 3, 1]
    Output: 0
    
    In this case, no transaction is done, i.e. max profit = 0.

    Solution one:

    This is array problem. The most simple solution is brute force with two level loop.

    For a price, loop till the end to find any price which is higher than it and update the max profit. Otherwise, return 0. Time complexity is O(n * n)

    class Solution {
    public:
        int maxProfit(vector<int>& prices) {
            if(prices.size() < 2){
                return 0;
            }
            int min = prices[0];
            int max_price = 0;
            int max_profit = 0;
            for(size_t i=0; i<prices.size()-1; i++){
                max_price = prices[i];
                for(size_t j=i+1; j<prices.size(); j++){
                    if( prices[j] > max_price){
                        max_price = prices[j];    
                    }
                }
                if((max_price - prices[i]) > max_profit){
                    max_profit = max_price - prices[i];
                }
            }
            return max_profit;
        }
    };

    Solution two: 

    In order to maximize the profit, we need a deep thinking. Actually, this problem asks the biggest difference between two numbers with the limitation that the lower number should in the front of the higher number in the array.

    We need to buy it at a low price and sell at high price. Keep two value: min price and max price. 

    For an element which is less than min value, it means we already find a local down and peak value. update the max profit, min price and max price.

    The time complexity is O(n)

    class Solution {
    public:
        int maxProfit(vector<int>& prices) {
            if(prices.empty()){
                return 0;
            }
            int max_val = prices[0];
            int min_val = prices[0];
            int max_profit = 0;
            for(int i = 1; i < prices.size(); i++){
                if(prices[i] < min_val){
                    max_profit = max(max_profit, max_val - min_val);
                    min_val = prices[i];
                    max_val = prices[i];
                } else {
                    max_val = max(max_val, prices[i]);
                }
            }
            max_profit = max(max_profit, max_val - min_val);
            return max_profit;
        }
    };

    The following code is a more concise version, but it is the same idea.

    class Solution {
    public:
        int maxProfit(vector<int>& prices) {
            int min_val = INT_MAX;
            int max_profit = 0;
            for(auto price : prices){
                min_val = min(min_val, price);
                max_profit = max(max_profit, price - min_val);
            }
            return max_profit;
        }
    };
  • 相关阅读:
    jquery插件课程1 幻灯片、城市选择、日期时间选择、拖放、方向拖动插件
    博客园随笔如何自动生成目录(原理:页脚js函数且执行)
    JAVA web四个属性的范围汇总
    关于继承modelDriven接口action的ajax来电参数
    Objective-C基调(4)Category
    Easyui使用记录
    jQuery地图热点效应-后在弹出的提示鼠标层信息
    跨境移动互联网的魅力演绎,hao123无论成就下一个条目?
    启示—地点IT高管20在职场心脏经(读书笔记6)
    C# 获得Excel工作簿Sheet页面(工作表)集合的名称
  • 原文地址:https://www.cnblogs.com/wdw828/p/6854888.html
Copyright © 2011-2022 走看看