Given an array of non-negative integers representing the elevations from the vertical cross section of a range of hills, determine how many units of snow could be captured between the hills.
See the example array and elevation map below.
[0, 1, 3, 0, 1, 2, 0, 4, 2, 0, 3, 0]
In this example, 13 units of snow (*) could be captured.
Implement computeSnowpack(arr) correctly.
这道题本质上是经典的“接雨水”问题:给定一组非负整数表示山丘的高度,要求计算两侧高峰之间可以积存多少单位的“雪”。核心思路通常是预处理每个位置左侧最高值和右侧最高值,再用当前位置可接高度 = min(左最高, 右最高) – 当前高度 累加得到答案;也可以用双指针在 O(n) 时间、O(1) 额外空间内完成。示例数组 [0,1,3,0,1,2,0,4,2,0,3,0] 的结果为 13,适合考察数组遍历、边界处理和贪心 / 双指针优化能力。