Skip to content

Commit 906095b

Browse files
authored
2025-01-19 v. 8.1.7: added "407. Trapping Rain Water II"
2 parents 56ab6b2 + 167e982 commit 906095b

File tree

4 files changed

+108
-1
lines changed

4 files changed

+108
-1
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -676,6 +676,7 @@ Profile on LeetCode: [fartem](https://leetcode.com/fartem/).
676676
| 297. Serialize and Deserialize Binary Tree | [Link](https://leetcode.com/problems/serialize-and-deserialize-binary-tree/) | [Link](./lib/hard/297_serialize_and_deserialize_binary_tree.rb) | [Link](./test/hard/test_297_serialize_and_deserialize_binary_tree.rb) |
677677
| 336. Palindrome Pairs | [Link](https://leetcode.com/problems/palindrome-pairs/) | [Link](./lib/hard/336_palindrome_pairs.rb) | [Link](./test/hard/test_336_palindrome_pairs.rb) |
678678
| 363. Max Sum of Rectangle No Larger Than K | [Link](https://leetcode.com/problems/max-sum-of-rectangle-no-larger-than-k/) | [Link](./lib/hard/363_max_sum_of_rectangle_no_larger_than_k.rb) | [Link](./test/hard/test_363_max_sum_of_rectangle_no_larger_than_k.rb) |
679+
| 407. Trapping Rain Water II | [Link](https://leetcode.com/problems/trapping-rain-water-ii/) | [Link](./lib/hard/407_trapping_rain_water_ii.rb) | [Link](./test/hard/test_407_trapping_rain_water_ii.rb) |
679680
| 458. Poor Pigs | [Link](https://leetcode.com/problems/poor-pigs/) | [Link](./lib/hard/458_poor_pigs.rb) | [Link](./test/hard/test_458_poor_pigs.rb) |
680681
| 871. Minimum Number of Refueling Stops | [Link](https://leetcode.com/problems/minimum-number-of-refueling-stops/) | [Link](./lib/hard/871_minimum_number_of_refueling_stops.rb) | [Link](./test/hard/test_871_minimum_number_of_refueling_stops.rb) |
681682
| 895. Maximum Frequency Stack | [Link](https://leetcode.com/problems/maximum-frequency-stack/) | [Link](./lib/hard/895_maximum_frequency_stack.rb) | [Link](./test/hard/test_895_maximum_frequency_stack.rb) |

leetcode-ruby.gemspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ require 'English'
55
::Gem::Specification.new do |s|
66
s.required_ruby_version = '>= 3.0'
77
s.name = 'leetcode-ruby'
8-
s.version = '8.1.6'
8+
s.version = '8.1.7'
99
s.license = 'MIT'
1010
s.files = ::Dir['lib/**/*.rb'] + %w[README.md]
1111
s.executable = 'leetcode-ruby'
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
# frozen_string_literal: true
2+
3+
# https://leetcode.com/problems/trapping-rain-water-ii/
4+
# @param {Integer[][]} height_map
5+
# @return {Integer}
6+
def trap_rain_water(height_map)
7+
trapped_water = 0
8+
water_level_map = ::Array.new(height_map.size) { ::Array.new(height_map[0].size, 20_000) }
9+
10+
(1...height_map.size - 1).each do |i|
11+
water_level_map[i][0] = height_map[i][0]
12+
13+
(1...height_map[i].size - 1).each { |j| water_level_map[i][j] = 20_000 }
14+
15+
water_level_map[i][height_map[i].size - 1] = height_map[i][height_map[i].size - 1]
16+
end
17+
18+
(0...height_map[0].size).each do |i|
19+
water_level_map[0][i] = height_map[0][i]
20+
water_level_map[height_map.size - 1][i] = height_map[height_map.size - 1][i]
21+
end
22+
23+
drain = true
24+
while drain
25+
drain = false
26+
(1...height_map[0].size - 1).each do |i|
27+
(1...height_map.size - 1).each do |j|
28+
next unless water_level_map[j][i] > height_map[j][i]
29+
30+
water_level_map[j][i] = [water_level_map[j][i - 1], height_map[j][i]].max if water_level_map[j][i] > water_level_map[j][i - 1]
31+
water_level_map[j][i] = [water_level_map[j - 1][i], height_map[j][i]].max if water_level_map[j][i] > water_level_map[j - 1][i]
32+
end
33+
end
34+
35+
(height_map[0].size - 2).downto(1) do |i|
36+
(height_map.size - 2).downto(1) do |j|
37+
next unless water_level_map[j][i] > height_map[j][i]
38+
39+
water_level_map[j][i] = [water_level_map[j][i + 1], height_map[j][i]].max if water_level_map[j][i] > water_level_map[j][i + 1]
40+
water_level_map[j][i] = [water_level_map[j + 1][i], height_map[j][i]].max if water_level_map[j][i] > water_level_map[j + 1][i]
41+
42+
next unless water_level_map[j][i] < water_level_map[j][i + 1] && water_level_map[j][i + 1] > height_map[j][i + 1] ||
43+
water_level_map[j][i] < water_level_map[j + 1][i] && water_level_map[j + 1][i] > height_map[j + 1][i]
44+
45+
drain = true
46+
end
47+
end
48+
end
49+
50+
(1...water_level_map.size - 1).each do |i|
51+
(1...water_level_map[i].size - 1).each do |j|
52+
trapped_water += water_level_map[i][j] - height_map[i][j]
53+
end
54+
end
55+
56+
trapped_water
57+
end
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
# frozen_string_literal: true
2+
3+
require_relative '../test_helper'
4+
require_relative '../../lib/hard/407_trapping_rain_water_ii'
5+
require 'minitest/autorun'
6+
7+
class TrappingRainWaterIITest < ::Minitest::Test
8+
def test_default_one
9+
assert_equal(
10+
4,
11+
trap_rain_water(
12+
[
13+
[1, 4, 3, 1, 3, 2],
14+
[3, 2, 1, 3, 2, 4],
15+
[2, 3, 3, 2, 3, 1]
16+
]
17+
)
18+
)
19+
end
20+
21+
def test_default_two
22+
assert_equal(
23+
10,
24+
trap_rain_water(
25+
[
26+
[3, 3, 3, 3, 3],
27+
[3, 2, 2, 2, 3],
28+
[3, 2, 1, 2, 3],
29+
[3, 2, 2, 2, 3],
30+
[3, 3, 3, 3, 3]
31+
]
32+
)
33+
)
34+
end
35+
36+
def test_additional_one
37+
assert_equal(
38+
3,
39+
trap_rain_water(
40+
[
41+
[5, 5, 5, 1],
42+
[5, 1, 1, 5],
43+
[5, 1, 5, 5],
44+
[5, 2, 5, 8]
45+
]
46+
)
47+
)
48+
end
49+
end

0 commit comments

Comments
 (0)