{"id":881,"date":"2021-04-13T20:09:58","date_gmt":"2021-04-14T03:09:58","guid":{"rendered":"http:\/\/cywang.no-ip.org\/wordpress\/?p=881"},"modified":"2021-04-13T20:09:58","modified_gmt":"2021-04-14T03:09:58","slug":"121-best-time-to-buy-and-sell-stock","status":"publish","type":"post","link":"http:\/\/cywang.no-ip.org\/wordpress\/?p=881","title":{"rendered":"121. Best Time to Buy and Sell Stock"},"content":{"rendered":"<p><a href=\"https:\/\/leetcode.com\/problems\/best-time-to-buy-and-sell-stock\/\" target=\"_blank\" rel=\"noopener noreferrer\">121. Best Time to Buy and Sell Stock<\/a><\/p>\n<p><!--more--><\/p>\n<pre class=\"line-numbers\"><code class=\"language-python\"># Approach 1. Brute Force\r\n# Time Complexity: O(n^2)\r\n# Space Complexity: O(1)\r\nclass Solution1:\r\n    def maxProfit(self, prices):\r\n        max_profit = 0\r\n        for i in range(len(prices)):\r\n            for j in range(i + 1, len(prices)):\r\n                profit = prices[j] - prices[i]\r\n                if profit &gt; max_profit:\r\n                    max_profit = profit\r\n        return max_profit\r\n\r\n\r\n# Approach 2. One loop\r\n# Time Complexity: O(n)\r\n# Space Complexity: O(1)\r\nclass Solution2:\r\n    def maxProfit(self, prices):\r\n        max_profit = 0\r\n        for i in range(len(prices)):\r\n            for j in range(i + 1, len(prices)):\r\n                profit = prices[j] - prices[i]\r\n                if profit &gt; max_profit:\r\n                    max_profit = profit\r\n        return max_profit<\/code><\/pre>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>121. Best Time to Buy and Sell Stock<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_exactmetrics_skip_tracking":false,"_exactmetrics_sitenote_active":false,"_exactmetrics_sitenote_note":"","_exactmetrics_sitenote_category":0,"footnotes":""},"categories":[39],"tags":[],"class_list":["post-881","post","type-post","status-publish","format-standard","hentry","category-leetcode"],"_links":{"self":[{"href":"http:\/\/cywang.no-ip.org\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/881","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/cywang.no-ip.org\/wordpress\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/cywang.no-ip.org\/wordpress\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/cywang.no-ip.org\/wordpress\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/cywang.no-ip.org\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=881"}],"version-history":[{"count":2,"href":"http:\/\/cywang.no-ip.org\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/881\/revisions"}],"predecessor-version":[{"id":884,"href":"http:\/\/cywang.no-ip.org\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/881\/revisions\/884"}],"wp:attachment":[{"href":"http:\/\/cywang.no-ip.org\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=881"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/cywang.no-ip.org\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=881"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/cywang.no-ip.org\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=881"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}