Thanks for your post! I've tried finding a simpler solution:
constmaxProfit=(prices)=>{if(prices.length===0)return0;// Calculate maximum profit for each price.constprofits=prices.map((price,i)=>{// Find maximum price, ignoring prices before our current price.constmax=Math.max(...prices.slice(i));returnmax-price;});// Return maximum profit.returnMath.max(...profits);};
I did not test for performance, though. Nevertheless, I think it's more readable.
I am a Senior Site Reliability Engineer passionate about making complex systems fast, resilient, and easy to operate. From managing 2k+ clusters in AWS to automating away thousands of alerts.
I think your solution is very clever using the spread operator to find the highest profit. I tried to share a solution where the step-by-step is very clear, so it can be rewritten in any language! Thanks again for yours, it is great!
Thanks for your post! I've tried finding a simpler solution:
I did not test for performance, though. Nevertheless, I think it's more readable.
I think your solution is very clever using the spread operator to find the highest profit. I tried to share a solution where the step-by-step is very clear, so it can be rewritten in any language! Thanks again for yours, it is great!
Thank you :)