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;
        }
    };
  • 相关阅读:
    Android “swipe” vs “fling”
    Parcel
    ViewConfiguration 和 ViewConfigurationCompat
    Hive记录-配置支持事务管理hive-site.xml
    SQL记录-资源正忙online或nowait
    SQL记录-解锁和dbms_job操作
    Java记录-SpringMVC整合Echarts画地图加散点图
    Hive记录-impala常用命令
    Hive记录-Sqoop常用命令
    Linux记录-shell一行代码杀死进程(收藏)
  • 原文地址:https://www.cnblogs.com/wdw828/p/6854888.html
Copyright © 2011-2022 走看看