forked from soapyigu/LeetCode-Swift
-
Notifications
You must be signed in to change notification settings - Fork 1
/
TrappingRainWater.swift
44 lines (37 loc) · 1.2 KB
/
TrappingRainWater.swift
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
/**
* Question Link: https://leetcode.com/problems/trapping-rain-water/
* Primary idea: The rain capacity is always determined by the the min value of
* left and right maximum height
* Time Complexity: O(n), Space Complexity: O(n)
*
*/
class TrappingRainWater {
func trap(height: [Int]) -> Int {
guard height.count > 0 else {
return 0
}
var res = 0
var left = _initMaxHeights(height, true)
var right = _initMaxHeights(height, false)
for i in 0 ..< height.count {
res += min(left[i], right[i]) - height[i]
}
return res
}
private func _initMaxHeights(height: [Int], _ isLeft: Bool) -> [Int] {
var res = [Int](count: height.count, repeatedValue: 0)
var currentMax = 0
if isLeft {
for i in 0 ..< height.count {
res[i] = max(currentMax, height[i])
currentMax = res[i]
}
} else {
for i in (0 ..< height.count).reverse() {
res[i] = max(currentMax, height[i])
currentMax = res[i]
}
}
return res
}
}