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;
        }
    };
  • 相关阅读:
    配置 dovecat 的 PAM
    通过CVE-2017-17215学习路由器漏洞分析,从入坑到放弃
    路由器漏洞复现分析第二弹:CNVD-2018-01084
    路由器漏洞复现分析第三弹:DVRF INTRO题目分析
    重新认识被人遗忘的HTTP头注入
    [转载]DLL劫持生成器 源码开放(纯WINDOWS SDK)+ 实例分析
    HOOK大法实现不修改程序代码给程序添加功能
    老树开新花:DLL劫持漏洞新玩法
    分享三个USB抓包软件---Bus Hound,USBlyzer 和-USBTrace
    网易云音乐PC客户端加密API逆向解析
  • 原文地址:https://www.cnblogs.com/wdw828/p/6854888.html
Copyright © 2011-2022 走看看