{"id":145,"date":"2017-09-03T20:42:14","date_gmt":"2017-09-03T12:42:14","guid":{"rendered":"https:\/\/www.ccagml.com\/?p=145"},"modified":"2018-11-27T16:42:55","modified_gmt":"2018-11-27T08:42:55","slug":"122-best-time-to-buy-and-sell-stock-ii","status":"publish","type":"post","link":"https:\/\/www.ccagml.com\/?p=145","title":{"rendered":"122. Best Time to Buy and Sell Stock II"},"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>Design an algorithm to find the maximum profit. You may complete as many transactions as you like (i.e., buy one and sell one share of the stock multiple times).<\/p>\n<p><strong>Note:<\/strong>\u00a0You may not engage in multiple transactions at the same time (i.e., you must sell the stock before you buy again).<\/p>\n<p><strong>Example 1:<\/strong><\/p>\n<pre><strong>Input:<\/strong> [7,1,5,3,6,4]\r\n<strong>Output:<\/strong> 7\r\n<strong>Explanation:<\/strong> Buy on day 2 (price = 1) and sell on day 3 (price = 5), profit = 5-1 = 4.\r\n\u00a0            Then buy on day 4 (price = 3) and sell on day 5 (price = 6), profit = 6-3 = 3.\r\n<\/pre>\n<p><strong>Example 2:<\/strong><\/p>\n<pre><strong>Input:<\/strong> [1,2,3,4,5]\r\n<strong>Output:<\/strong> 4\r\n<strong>Explanation:<\/strong> Buy on day 1 (price = 1) and sell on day 5 (price = 5), profit = 5-1 = 4.\r\n\u00a0            Note that you cannot buy on day 1, buy on day 2 and sell them later, as you are\r\n\u00a0            engaging multiple transactions at the same time. You must sell before buying again.\r\n<\/pre>\n<p><strong>Example 3:<\/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        # [1,2,3]\r\n        if not prices:\r\n            return 0\r\n        result = 0\r\n        current_price = prices[0]\r\n        for i in range(len(prices)):\r\n            if current_price &lt;= prices[i]:\r\n                result += (current_price-prices[i])\r\n            current_price = prices[i]\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=145\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">122. Best Time to Buy and Sell Stock II<\/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\/145"}],"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=145"}],"version-history":[{"count":1,"href":"https:\/\/www.ccagml.com\/index.php?rest_route=\/wp\/v2\/posts\/145\/revisions"}],"predecessor-version":[{"id":146,"href":"https:\/\/www.ccagml.com\/index.php?rest_route=\/wp\/v2\/posts\/145\/revisions\/146"}],"wp:attachment":[{"href":"https:\/\/www.ccagml.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=145"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.ccagml.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=145"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.ccagml.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=145"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}