Skip to content

Instantly share code, notes, and snippets.

@muxcmux
Created January 23, 2020 22:35
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 muxcmux/27159814ff7e3d6b6cf877362f8de440 to your computer and use it in GitHub Desktop.
Save muxcmux/27159814ff7e3d6b6cf877362f8de440 to your computer and use it in GitHub Desktop.
wtf is the complexity for this shit?!
# @param {Integer[]} prices
# @return {Integer}
def max_profit(prices)
mp = 0
prices.each_with_index do |price, i|
for n in (i + 1)..prices.count do
profit = prices[n].to_i - price
mp = profit if profit > mp
end
end
mp
end
puts max_profit([7,1,5,3,6,4])
puts max_profit([7,6,4,3,1])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment