Skip to content
Prev Previous commit
1765
  • Loading branch information
theforestvn88 committed Jul 31, 2024
commit b3a2d512c054b21560985228c69b00b0319e0d89
32 changes: 32 additions & 0 deletions 1701-1800/1765_map_of_highest_peak.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# @param {Integer[][]} is_water
# @return {Integer[][]}
def highest_peak(is_water)
r = is_water.size
c = is_water[0].size
result = Array.new(r) { Array.new(c, Float::INFINITY) }

queue = []
(0...r).each { |i|
(0...c).each { |j|
if is_water[i][j] == 1
result[i][j] = 0
queue << [i, j]
end
}
}

directions = [[-1, 0], [1, 0], [0, -1], [0, 1]]
until queue.empty?
i, j = queue.shift
directions.each { |dr, dc|
_i = i + dr
_j = j + dc
next if _i < 0 || _i >= r || _j < 0 || _j >= c

queue << [_i, _j] if result[_i][_j] == Float::INFINITY
result[_i][_j] = [result[_i][_j], result[i][j] + 1].min
}
end

result
end