diff --git a/lcci/17.21.Volume of Histogram/README.md b/lcci/17.21.Volume of Histogram/README.md index f7efe350feec0..7b4ffd2cfe962 100644 --- a/lcci/17.21.Volume of Histogram/README.md +++ b/lcci/17.21.Volume of Histogram/README.md @@ -156,6 +156,38 @@ public class Solution { } ``` +```swift +class Solution { + func trap(_ height: [Int]) -> Int { + let n = height.count + if n < 3 { + return 0 + } + + var left = [Int](repeating: 0, count: n) + var right = [Int](repeating: 0, count: n) + + left[0] = height[0] + right[n - 1] = height[n - 1] + + for i in 1.. diff --git a/lcci/17.21.Volume of Histogram/README_EN.md b/lcci/17.21.Volume of Histogram/README_EN.md index c934f608d3a0d..a0557cd372146 100644 --- a/lcci/17.21.Volume of Histogram/README_EN.md +++ b/lcci/17.21.Volume of Histogram/README_EN.md @@ -150,6 +150,38 @@ public class Solution { } ``` +```swift +class Solution { + func trap(_ height: [Int]) -> Int { + let n = height.count + if n < 3 { + return 0 + } + + var left = [Int](repeating: 0, count: n) + var right = [Int](repeating: 0, count: n) + + left[0] = height[0] + right[n - 1] = height[n - 1] + + for i in 1.. diff --git a/lcci/17.21.Volume of Histogram/Solution.swift b/lcci/17.21.Volume of Histogram/Solution.swift new file mode 100644 index 0000000000000..61d30a3e741d4 --- /dev/null +++ b/lcci/17.21.Volume of Histogram/Solution.swift @@ -0,0 +1,29 @@ +class Solution { + func trap(_ height: [Int]) -> Int { + let n = height.count + if n < 3 { + return 0 + } + + var left = [Int](repeating: 0, count: n) + var right = [Int](repeating: 0, count: n) + + left[0] = height[0] + right[n - 1] = height[n - 1] + + for i in 1..