minprice
to the first element of the prices
array, representing the minimum price seen so far.maxprofit
to 0, representing the maximum profit seen so far.prices
array starting from the second element.
a. Update minprice
if the current price is less than minprice
.
b. Calculate the potential profit by subtracting minprice
from the current price.
c. Update maxprofit
if the potential profit is greater than maxprofit
.maxprofit
as the maximum profit that can be achieved.