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 software engineer with a background in business and science who finally discovered programming and became a Software Engineer (best thing ever!).
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 :)