forked from mohitjain/leetcode_solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path42_trapping_rain_water.rb
More file actions
55 lines (50 loc) · 1.61 KB
/
42_trapping_rain_water.rb
File metadata and controls
55 lines (50 loc) · 1.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# Leetcode Problem: https://leetcode.com/problems/trapping-rain-water/
# Given n non-negative integers representing an elevation map where the width of each bar is 1, compute how much water it is able to trap after raining.
#
#
# The above elevation map is represented by array [0,1,0,2,1,0,1,3,2,1,2,1]. In this case, 6 units of rain water (blue section) are being trapped. Thanks Marcos for contributing this image!
#
# Example:
#
# Input: [0,1,0,2,1,0,1,3,2,1,2,1]
# Output: 6
# ----------------------------------------------------------------------------------------------------------------------
# @param {Integer[]} height
# @return {Integer}
def trap(heights)
left_max_heights = []
right_max_heights = []
left_max = nil
heights.each_with_index do |height, index|
if left_max.nil?
left_max = height
left_max_heights[0] = height
else
left_max = [left_max, height].max
end
left_max_heights[index + 1] = left_max if index < heights.length - 1
end
right_max = nil
index = heights.length - 1
heights.reverse_each do |height|
if right_max.nil?
right_max = height
right_max_heights[index] = height
else
right_max = [right_max, height].max
end
right_max_heights[index - 1] = right_max if index.positive?
index -= 1
end
output = 0
p left_max_heights
p right_max_heights
heights.each_with_index do |_height, index|
current_trapped_water = [left_max_heights[index], right_max_heights[index]].min - heights[index]
output += current_trapped_water if current_trapped_water.positive?
end
output
end
p trap(
[0,1,0,2,1,0,1,3,2,1,2,1]
)