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;
        }
    };
  • 相关阅读:
    关于ADO.NET参数化查询的提问
    PushSharp的使用
    关于WCF的一些知识点
    关于Expression表达式树的拼接
    设计模式-策略模式
    jq图片展示插件highslide.js简单dom
    JS实现手机访问pc网址自动跳转到wap网站
    CARVARS 圆形进度条
    vue-router的几种用法
    vscode 超好用的前端插件
  • 原文地址:https://www.cnblogs.com/wdw828/p/6854888.html
Copyright © 2011-2022 走看看