alternative solution to maxProfit using constant memory