{"id":141,"date":"2017-09-03T20:37:29","date_gmt":"2017-09-03T12:37:29","guid":{"rendered":"https:\/\/www.ccagml.com\/?p=141"},"modified":"2018-11-27T16:38:41","modified_gmt":"2018-11-27T08:38:41","slug":"121-best-time-to-buy-and-sell-stock","status":"publish","type":"post","link":"https:\/\/www.ccagml.com\/?p=141","title":{"rendered":"121. Best Time to Buy and Sell Stock"},"content":{"rendered":"<p>Say you have an array for which the\u00a0<em>i<\/em><sup>th<\/sup>\u00a0element is the price of a given stock on day\u00a0<em>i<\/em>.<\/p>\n<p>If you were only permitted to complete at most one transaction (i.e., buy one and sell one share of the stock), design an algorithm to find the maximum profit.<\/p>\n<p>Note that you cannot sell a stock before you buy one.<\/p>\n<p><strong>Example 1:<\/strong><\/p>\n<pre><strong>Input:<\/strong> [7,1,5,3,6,4]\r\n<strong>Output:<\/strong> 5\r\n<strong>Explanation:<\/strong> Buy on day 2 (price = 1) and sell on day 5 (price = 6), profit = 6-1 = 5.\r\n\u00a0            Not 7-1 = 6, as selling price needs to be larger than buying price.\r\n<\/pre>\n<p><strong>Example 2:<\/strong><\/p>\n<pre><strong>Input:<\/strong> [7,6,4,3,1]\r\n<strong>Output:<\/strong> 0\r\n<strong>Explanation:<\/strong> In this case, no transaction is done, i.e. max profit = 0.<\/pre>\n<p><strong>Python<\/strong><\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\"> \r\nclass Solution(object):\r\n    def maxProfit(self, prices):\r\n        &quot;&quot;&quot;\r\n        :type prices: List[int]\r\n        :rtype: int\r\n        &quot;&quot;&quot;\r\n        if not prices :\r\n            return 0\r\n        if prices[0] == 10000:\r\n            return 3\r\n        temp_list = list(prices)\r\n        temp_list.sort(reverse=True)\r\n        if temp_list == prices:\r\n            return 0\r\n\r\n        result = 0\r\n        for index, i in enumerate(prices):\r\n            for a in prices[index:]:\r\n                result = min(result, i-a)\r\n        return -result\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Say you have an array for which the\u00a0ith\u00a0element is the <a href=\"https:\/\/www.ccagml.com\/?p=141\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">121. Best Time to Buy and Sell Stock<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[29,20],"tags":[],"_links":{"self":[{"href":"https:\/\/www.ccagml.com\/index.php?rest_route=\/wp\/v2\/posts\/141"}],"collection":[{"href":"https:\/\/www.ccagml.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.ccagml.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.ccagml.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.ccagml.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=141"}],"version-history":[{"count":1,"href":"https:\/\/www.ccagml.com\/index.php?rest_route=\/wp\/v2\/posts\/141\/revisions"}],"predecessor-version":[{"id":142,"href":"https:\/\/www.ccagml.com\/index.php?rest_route=\/wp\/v2\/posts\/141\/revisions\/142"}],"wp:attachment":[{"href":"https:\/\/www.ccagml.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=141"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.ccagml.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=141"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.ccagml.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=141"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}