(https://github.githubassets.com) (https://avatars.githubusercontent.com) (https://github-cloud.s3.amazonaws.com) (https://user-images.githubusercontent.com/) (https://github.githubassets.com) (https://avatars.githubusercontent.com) (https://github.githubassets.com/assets/light-3e154969b9f9.css) (https://github.githubassets.com/assets/dark-9c5b7a476542.css) (https://github.githubassets.com/assets/primer-primitives-4cf0d59ab51a.css) (https://github.githubassets.com/assets/primer-af846850481e.css) (https://github.githubassets.com/assets/global-8b10f05a77e6.css) (https://github.githubassets.com/assets/github-2f6e722088eb.css) Buy/Sell · GitHub (gist_code) (https://github.githubassets.com/) (/opensearch-gist.xml) (Gist) (https://gist.github.com/fluidicon.png) (GitHub) (/avimoondra.atom) (atom) (https://github.githubassets.com/assets/gist-622547bb175b.css) (https://github.githubassets.com/assets/pinned-octocat-093da3e6fa40.svg) (https://github.githubassets.com/favicons/favicon.png) (https://github.githubassets.com/favicons/favicon.svg) Skip to content (https://github.githubassets.com/assets/primer-react.9fa170e9435ed4b922b9.module.css) (/) (Search…) (/discover) All gists (https://github.com) Back to GitHub (https://gist.github.com/auth/github?return_to=https%3A%2F%2Fgist.github.com%2Favimoondra%2Fc580ccefb8f0e46d31b49902aed09080) Sign in (/join?return_to=https%3A%2F%2Fgist.github.com%2Favimoondra%2Fc580ccefb8f0e46d31b49902aed09080&source=header-gist) Sign up (/) (https://gist.github.com/auth/github?return_to=https%3A%2F%2Fgist.github.com%2Favimoondra%2Fc580ccefb8f0e46d31b49902aed09080) Sign in (/join?return_to=https%3A%2F%2Fgist.github.com%2Favimoondra%2Fc580ccefb8f0e46d31b49902aed09080&source=header-gist) Sign up You signed in with another tab or window. () Reload to refresh your session. You signed out in another tab or window. () Reload to refresh your session. You switched accounts on another tab or window. () Reload to refresh your session. Dismiss alert Instantly share code, notes, and snippets. (/avimoondra) (@avimoondra) (/avimoondra) avimoondra / (/avimoondra/c580ccefb8f0e46d31b49902aed09080) spec.md (Only those with the link can see this gist.) Secret Last active (Jun 8, 2021, 5:43 PM UTC) June 8, 2021 17:43 Show Gist options (/avimoondra/c580ccefb8f0e46d31b49902aed09080/archive/22299efd1c4ae19c280134b1b6f78e14011dd995.zip) Download ZIP (/login?return_to=https%3A%2F%2Fgist.github.com%2Favimoondra%2Fc580ccefb8f0e46d31b49902aed09080) Star ((0) 0 ) (0) 0 You must be signed in to star a gist (/login?return_to=https%3A%2F%2Fgist.github.com%2Favimoondra%2Fc580ccefb8f0e46d31b49902aed09080) Fork ((0) 0 ) (0) 0 You must be signed in to fork a gist Embed () Embed Embed this gist in your website. (https://gist.github.com/avimoondra/c580ccefb8f0e46d31b49902aed09080) Share Copy sharable link for this gist. (https://gist.github.com/c580ccefb8f0e46d31b49902aed09080.git) Clone via HTTPS Clone using the web URL. (https://docs.github.com/articles/which-remote-url-should-i-use) Learn more about clone URLs Clone this repository at <script src="https://gist.github.com/avimoondra/c580ccefb8f0e46d31b49902aed09080.js"></script> () (https://desktop.github.com) Save avimoondra/c580ccefb8f0e46d31b49902aed09080 to your computer and use it in GitHub Desktop. (/avimoondra/c580ccefb8f0e46d31b49902aed09080) Code (/avimoondra/c580ccefb8f0e46d31b49902aed09080/revisions) Revisions (11) 11 Embed () Embed Embed this gist in your website. (https://gist.github.com/avimoondra/c580ccefb8f0e46d31b49902aed09080) Share Copy sharable link for this gist. (https://gist.github.com/c580ccefb8f0e46d31b49902aed09080.git) Clone via HTTPS Clone using the web URL. (https://docs.github.com/articles/which-remote-url-should-i-use) Learn more about clone URLs Clone this repository at <script src="https://gist.github.com/avimoondra/c580ccefb8f0e46d31b49902aed09080.js"></script> () (https://desktop.github.com) Save avimoondra/c580ccefb8f0e46d31b49902aed09080 to your computer and use it in GitHub Desktop. (/avimoondra/c580ccefb8f0e46d31b49902aed09080/archive/22299efd1c4ae19c280134b1b6f78e14011dd995.zip) Download ZIP Buy/Sell (/avimoondra/c580ccefb8f0e46d31b49902aed09080/raw/22299efd1c4ae19c280134b1b6f78e14011dd995/spec.md) Raw spec.md Buy/Sell Given a list of stock prices (ordered by time), we want to write a function for the max profit you can make from buying the stock at one point in time, and selling it a later point in time. In the example below, we'd return a profit of $10 by buying at $1 and selling at $11. Function: buy_sell(prices: Number[])
Input: [3, 5, 1, 7, 8, 10, 11, 2]
Output: [10, 1, 11]
Example: buy_sell([3, 5, 1, 7, 8, 10, 11, 2]) == [10, 1, 11] Requirements: You cannot buy and sell in the same day You must buy before you sell (i.e. no shorts) You must make exactly 1 trade If the prices are decreasing, we'd minimize loss (return the least negative profit) Stock prices will always be an array of positive integers, with at least 2 prices Output format: [, , ] Space constraints : O(n), n = # of prices Run time constraints : None. There is no penalty for greater than O(n) run time. In fact, we prefer arriving more quickly at a solution that's optimized for readability, NOT run time. (/join?source=comment-gist) Sign up for free to join this conversation on GitHub .
Already have an account? (/login?return_to=https%3A%2F%2Fgist.github.com%2Favimoondra%2Fc580ccefb8f0e46d31b49902aed09080) Sign in to comment Footer (GitHub) (https://github.com) © 2024 GitHub, Inc. Footer navigation (https://docs.github.com/site-policy/github-terms/github-terms-of-service) Terms (https://docs.github.com/site-policy/privacy-policies/github-privacy-statement) Privacy (https://github.com/security) Security (https://www.githubstatus.com/) Status (https://docs.github.com/) Docs (https://support.github.com?tags=dotcom-footer) Contact Manage cookies Do not share my personal information You can’t perform that action at this time.