Skip to content

Instantly share code, notes, and snippets.

@ncaron
Created January 13, 2018 05:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ncaron/1125b59c8969810fe8d9844db926f842 to your computer and use it in GitHub Desktop.
Save ncaron/1125b59c8969810fe8d9844db926f842 to your computer and use it in GitHub Desktop.
Stock Picker solution from The Odin Project
def stock_picker prices
best_days = []
profit = 0
i = 0
while i < prices.length - 1
j = i
while j < prices.length
possible_profit = prices[j] - prices[i]
if possible_profit > profit
profit = possible_profit
best_days = [i, j]
end
j += 1
end
i += 1
end
best_days
end
p stock_picker([17,3,6,9,15,8,6,1,10]) == [1, 4]
p stock_picker([1, 15]) == [0, 1]
p stock_picker([15, 1]) == []
p stock_picker([15, 1, 2, 3]) == [1, 3]
p stock_picker([15, 2, 2, 3]) == [1, 3]
p stock_picker([15, 3, 2, 3]) == [2, 3]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment