{"id":189,"date":"2019-01-31T15:02:21","date_gmt":"2019-01-31T20:02:21","guid":{"rendered":"https:\/\/resrvoir.com\/?page_id=189"},"modified":"2019-03-17T13:49:13","modified_gmt":"2019-03-17T17:49:13","slug":"maximum-gain","status":"publish","type":"page","link":"https:\/\/resrvoir.com\/?page_id=189","title":{"rendered":"Maximum Gain"},"content":{"rendered":"\n<p>Given an array of price fluctuations throughout the day for a stock in the stock market, find the buy and sell price that would have yielded the maximum gain for the day.<br><br>Hint: the maximum gain is not necessarily related to the high price or low price of the day. To make a gain, a buy needs to occur before a sell, and the buy price must be lower than the sell price.<br><br>This can be solved in one pass through the prices, or O(n).<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"kotlin\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">var buy = 0\nvar sell = 0\nvar max = 0\nvar low = prices[0]\nvar high = prices[0]\n\nfor i in 1 ..&lt; prices.count {\n  let price = prices[i]\n  \n  if price - low > max {\n    max = price - low\n    sell = price\n    buy = low\n  }\n  \n  if price &lt; low {\n    low = price\n  }\n  \n  if price > high {\n    high = price\n  }\n}\n\nprint(\"buy \\(buy) sell \\(sell)\")\nprint(\"low \\(low) high \\(high)\")\nprint(\"max \\(max)\")<\/pre>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Given an array of price fluctuations throughout the day for a stock in the stock market, find the buy and sell price that would have yielded the maximum gain for the day. Hint: the maximum gain is not necessarily related to the high price or low price of the day. To make a gain, a [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":2,"menu_order":10,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"_links":{"self":[{"href":"https:\/\/resrvoir.com\/index.php?rest_route=\/wp\/v2\/pages\/189"}],"collection":[{"href":"https:\/\/resrvoir.com\/index.php?rest_route=\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/resrvoir.com\/index.php?rest_route=\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/resrvoir.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/resrvoir.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=189"}],"version-history":[{"count":9,"href":"https:\/\/resrvoir.com\/index.php?rest_route=\/wp\/v2\/pages\/189\/revisions"}],"predecessor-version":[{"id":719,"href":"https:\/\/resrvoir.com\/index.php?rest_route=\/wp\/v2\/pages\/189\/revisions\/719"}],"up":[{"embeddable":true,"href":"https:\/\/resrvoir.com\/index.php?rest_route=\/wp\/v2\/pages\/2"}],"wp:attachment":[{"href":"https:\/\/resrvoir.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=189"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}