## 121. Best Time to Buy and Sell Stock <br> :::spoiler **Optimal Space & Time Complexity** ``` - Time complexity: - Space complexity: ``` ::: <br> <hr/> ## Solutions :::spoiler 東 ```javascript= ``` ::: <br> :::spoiler Hao ```javascript= ``` ::: <br> :::spoiler YC ```javascript= ``` ::: <br> :::spoiler SOL ```javascript= ``` ::: --- ## Supplement / Discussion ### 東 ### Hao https://leetcode.com/problems/best-time-to-buy-and-sell-stock/solutions/1735550/python-javascript-easy-solution-with-very-clear-explanation/ 1. 直覺會覺得使用左右指針來做,left 找最小值,right 找最大值 2. 更新的邏輯 1. Fix left 1. Right > left => update maxProfit 2. Right < left => left = right 2. Loop ends when right < prices.length ### YC ### SOL
×
Sign in
Email
Password
Forgot password
or
By clicking below, you agree to our
terms of service
.
Sign in via Facebook
Sign in via Twitter
Sign in via GitHub
Sign in via Dropbox
Sign in with Wallet
Wallet (
)
Connect another wallet
New to HackMD?
Sign up