Last active
September 18, 2015 09:15
-
-
Save eva0919/20f92c8b575c9416579c to your computer and use it in GitHub Desktop.
Piccollage question#2
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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