LeetCode Solution - 121 Best time to Buy and Sell Stock | Javascript

  Рет қаралды 10,971

ThinkFWD

ThinkFWD

Күн бұрын

Пікірлер: 24
@david_yuzhakov
@david_yuzhakov Жыл бұрын
Thanks, bro, I haven't found a simple and high-quality video like yours anywhere.
@ThinkFWD
@ThinkFWD 5 жыл бұрын
Please feel free to leave a comment and let me know what you think !
@ThinkFWD
@ThinkFWD 4 жыл бұрын
From user @ leetcode @dermatobia The solution above could be improved by not actually changing the input array to cache profits. we could actually remove a few lines of code and achieve the same output! .. we don't actually have to cache the profits in our prices array. Since the profit var does it indirectly already. :) var maxProfit = function(prices) { let buy = prices[0]; let profit = 0; for (let i = 0; i < prices.length; i++) { if (buy > prices[i]) { buy = prices[i]; } else { profit = Math.max(prices[i]-buy, profit); } } return profit; };
@sawyerburnett8319
@sawyerburnett8319 Жыл бұрын
Good call. I was about to say this. In this case it doesn't add any value to update the array, and could cause confusion or bugs in the solution.
@jerryliu862
@jerryliu862 2 жыл бұрын
I appreciate you sharing this video, It improves my coding skill.
@edetmmekut809
@edetmmekut809 3 жыл бұрын
Hi really cool explaination ,but u dont need to mutate the array that extra over head cost
@riturathinsharma5931
@riturathinsharma5931 3 жыл бұрын
This is a great solution mate , but the best solution is “divide and conquer”.
@initialb811
@initialb811 3 жыл бұрын
great stuff! small recommendation: can you add comments in your code as a way for viewers to follow along better? I find myself needing to scrub back a couple of times to review why something was written. This video is definitely easy enough to follow, but it's something I find myself needing to do since I'm binge watching your videos. =D Also, subscribed! Thanks for making these videos!
@ibeeliot
@ibeeliot 4 жыл бұрын
i binge on your videos. keep it up.
@ThinkFWD
@ThinkFWD 4 жыл бұрын
will release one tonight :) stay tuned !
@kathiateran5826
@kathiateran5826 3 жыл бұрын
Really appreciate your videos, you're great at explaining things!
@ThinkFWD
@ThinkFWD 3 жыл бұрын
Thanks! Appreciate it
@DmitriyMalayevProfile
@DmitriyMalayevProfile 2 жыл бұрын
You're an awesome instructor. Can you please check why the volume is so low?
@pinkhairblackman8141
@pinkhairblackman8141 3 жыл бұрын
reason he set price[i]=0 is the edge case where you can never make a profit
@nurlanikhsan3692
@nurlanikhsan3692 Жыл бұрын
Nice
@ThinkFWD
@ThinkFWD Жыл бұрын
Thanks
@kidoo1567
@kidoo1567 Жыл бұрын
U are best
@rakeshrockie3213
@rakeshrockie3213 4 жыл бұрын
please solve more problems. love your videos
@ThinkFWD
@ThinkFWD 4 жыл бұрын
will do ! planning on putting a few more stay tuned
@OmarAhmed-xc7bs
@OmarAhmed-xc7bs 3 ай бұрын
Nice work and explanation but please remove the music, it is a big distraction, tysm.
@EshsUrbanPulseYT
@EshsUrbanPulseYT 7 ай бұрын
How this is dynamic programming, could anyone please explain?
@KolaIL
@KolaIL 2 жыл бұрын
var maxProfit = function(prices) { let minprice = prices[0]; let profit = 0; for (let i = 0; i < prices.length; i++) { if (prices[i] < minprice) { minprice = prices[i]; } else if (prices[i] - minprice > profit) { profit = prices[i] - minprice; } } return profit };
@user-zp1dv4yh5e
@user-zp1dv4yh5e 2 жыл бұрын
Hey, I tried you appraoach , but it's not working function findMaxProfit2(prices){ let buy = [0] prices[0] = 0 let profit = 0 for(let i = 1; i < prices.length ;i++){ if(buy > prices[i] ){ buy = prices[i] price[i] = 0 }else{ profit = Math.max(prices[i]-buy, profit) } } return profit }
快乐总是短暂的!😂 #搞笑夫妻 #爱美食爱生活 #搞笑达人
00:14
朱大帅and依美姐
Рет қаралды 13 МЛН
Accompanying my daughter to practice dance is so annoying #funny #cute#comedy
00:17
Funny daughter's daily life
Рет қаралды 20 МЛН
I thought one thing and the truth is something else 😂
00:34
عائلة ابو رعد Abo Raad family
Рет қаралды 9 МЛН
ТЮРЕМЩИК В БОКСЕ! #shorts
00:58
HARD_MMA
Рет қаралды 2,7 МЛН
DP 36. Buy and Sell Stock - II | Recursion to Space Optimisation
35:34
take U forward
Рет қаралды 249 М.
49. Group Anagrams | Javascript
18:58
ThinkFWD
Рет қаралды 6 М.
DP 35. Best Time to Buy and Sell Stock | DP on Stocks 🔥
9:11
take U forward
Рет қаралды 412 М.
Machine Learning for Everybody - Full Course
3:53:53
freeCodeCamp.org
Рет қаралды 8 МЛН
一个视频解决所有Best Time to Buy and Sell Stock问题
23:47
快乐总是短暂的!😂 #搞笑夫妻 #爱美食爱生活 #搞笑达人
00:14
朱大帅and依美姐
Рет қаралды 13 МЛН