DEV Community

Discussion on: Write better code: Day 1 - Apple Stock Prices

Collapse
 
arjunrajkumar profile image
Arjun Rajkumar • Edited

This was my logic for doing this.

Logic:

  • Go thru each price one by one.
  • See if the current stock is lesser than previous min_stock stored variable.
  • See if the profit is greater than max profit stored. (max_profit, pot_profit)
  • Going thru the loop once so it is O[n] time
  • O[1] space as storing fixed.

It works with these few example cases:
Examples: [10, 7, 5, 8, 11, 9] -> 6 ; [10, 7, 100, 8, 11, 9] -> 92 ; [10] -> 0 ; [10, 2] -> 0.

Code:
alt text

Github