bugfree Icon
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course

Leetcode Problem 121. Best Time to Buy and Sell Stock

121. Best Time to Buy and Sell Stock

Leetcode Solutions

One Pass Approach to Maximize Stock Profit

  1. Initialize minprice to the first element of the prices array, representing the minimum price seen so far.
  2. Initialize maxprofit to 0, representing the maximum profit seen so far.
  3. Iterate through the 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.
  4. Return maxprofit as the maximum profit that can be achieved.
UML Thumbnail

Ask Question

Programming Language
image/screenshot of info(optional)
Full Screen
Loading...

Suggested Answer

Answer
Full Screen
Copy Answer Code
Loading...