Skip to content

Instantly share code, notes, and snippets.

@eva0919
Last active September 18, 2015 09:15
Show Gist options
  • Save eva0919/20f92c8b575c9416579c to your computer and use it in GitHub Desktop.
Save eva0919/20f92c8b575c9416579c to your computer and use it in GitHub Desktop.
Piccollage question#2
def goodPoint(arr)
arrSize = arr.size
tempArr = Array.new(arrSize-1)
tempArr.each_index do |ind|
tempArr[ind] = arr[ind+1] - arr[ind]
end
maxNumber = 0
tempMaxNumber = 0
flag = 0
(0..arrSize-2).each do |i|
if tempArr[i]+tempMaxNumber > maxNumber
tempMaxNumber += tempArr[i]
maxNumber = tempMaxNumber
#if flag == 0
flag = i
#end
elsif tempArr[i]+tempMaxNumber > 0
tempMaxNumber += tempArr[i]
else
tempMaxNumber = 0
#flag = 0
end
end
puts "Buy the stock at $#{( arr[flag+1] - maxNumber )}\nand then sell it at $#{arr[flag+1]}\nfor a profit of $#{maxNumber}"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment