Created
October 8, 2019 15:03
-
-
Save abdellani/f7387cf14a21ce7f89d1e6253171c2e6 to your computer and use it in GitHub Desktop.
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 getNext (x,y,map) | |
col=map.first.length | |
row=map.length | |
max=map[x][y] | |
next_cell=[x,y] | |
if 0<x and map[x-1][y]> max | |
max=map[x-1][y] | |
next_cell=[x-1,y] | |
end | |
if 0<y and map[x][y-1]> max | |
max=map[x][y-1] | |
next_cell=[x,y-1] | |
end | |
if x<row-1 and map[x+1][y]> max | |
max=map[x+1][y] | |
next_cell=[x+1,y] | |
end | |
if y<col-1 and map[x][y+1]> max | |
max=map[x][y+1] | |
next_cell=[x,y+1] | |
end | |
return nil if next_cell[0]==x && next_cell[1]==y | |
next_cell | |
end | |
def dfs (x,y,map) | |
local_peak=[x,y] | |
while true | |
res= getNext(local_peak[0],local_peak[1],map) | |
local_peak=res unless res.nil? | |
return local_peak if res.nil? | |
end | |
end | |
def greatest_peaks(map) | |
# write your code here | |
peaks=Hash.new 0 | |
map.length.times do |i| | |
map.first.length.times do |j| | |
peaks[dfs(i,j,map)]+=1 | |
end | |
end | |
min=nil | |
max=nil | |
peaks.each do |key,value| | |
min = value if min.nil? or value<min | |
max = value if max.nil? or value>max | |
end | |
[min,max] | |
end | |
greatest_peaks( | |
[ | |
[9, 8, 5], | |
[5, 6, 3], | |
[8, 4, 1] | |
] | |
) | |
# => [3, 6] | |
=begin | |
p greatest_peaks( | |
[ | |
[8, 12], | |
[9, 3] | |
] | |
) | |
# => [1, 3] | |
=end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment