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;
        }
    };
  • 相关阅读:
    Django-website 程序案例系列-3 URL详解
    Django-website 程序案例系列-1 最简单的web服务器
    c# 多维数组、交错数组(转化为DataTable)
    c++(重载等号=操作为深拷贝)
    c# 导入c++ dll
    Nhibernate HQL 匿名类(严格说是map的使用以及构造函数的使用
    spring.net 集成nhibernate配置文件(这里暴露了GetCurrentSession 对于 CurrentSession unbond thread这里给出了解决方法)
    hibernate mapping文件中 xmlns会导致linq to xml 查询不到对应的节点
    linq to xml
    xml 操作(动态添加 property属性 其他节点同理)
  • 原文地址:https://www.cnblogs.com/wdw828/p/6854888.html
Copyright © 2011-2022 走看看