Trapping Rain Water II

Givenn_x_m_non-negative integers representing an elevation map 2d where the area of each cell is_1_x_1, compute how much water it is able to trap after raining.

分析:

从外围最低点向内灌水 外面形成墙,保证灌水高度不会漏

1 最外围都加入堆,从外往内灌。

2 用堆维护最小值(override compare) 堆里加入三元组(x,y,当前灌水最高高度)

时间复杂度log(2m+2n)*n*m

农村包围城市,无需双重for loop 依然遍历了所有点,这种从外围然后四点扩展法值得注意。

答案:

Python

Last updated

Was this helpful?