# [1, -3, 5, 2]
# max slice is [1, 5] so from index 0 to 2
def solution(a)
  # keep track of: old max, max_here
  max_old = 0
  max_here = 0

  # [-3, 5, 2]
  a.drop(1).each_index do |i|
    diff = a[i] - a[i-1]
    #diff = 5 - (-3) = 8
      #diff = 2 - 5 = -3
    # if diff > 0, redefine max_here, else set to 0.
    temp = max_old + diff
      #temp = 8 - 3 = 5
    max_here = diff > 0 ? temp : 0
    #max_here = (0 + 8) > 0 ? 8 : 0 => 0
      #max_here = (-3 > 0) ? temp : 0 => 0
    #max_old = 8 > 0 ? 8 : 0 => 8
      # max_old = 0 ? 8 ? 0 : 8 => 8
    max_old = max_here > max_old ? max_here : max_old
  end
  puts max_old
  max_old
end

def solution(a)
  return 0 if a.size < 2
  return 0 if a.size == 2 && a[1] <= a[0]

  low_so_far = a.first
  max_profit = 0
  # for each price in a
  a.each do |price|
    # set max_profit to price-low_so_far if that difference is greater than the current max_profit
    diff_here = price - low_so_far
    max_profit = diff_here if diff_here > max_profit
    low_so_far = price if price < low_so_far
  end
  max_profit
end

Add max_profit solution in ruby
Add max_profit solution in ruby
