From 0bfc4c7cea129d206334c3191b2c761458825efa Mon Sep 17 00:00:00 2001 From: Lanre Adedara Date: Mon, 13 May 2024 08:07:40 +0100 Subject: [PATCH 1/3] Swift Implementation for LCCI 17.21 --- lcci/17.21.Volume of Histogram/Solution.swift | 29 +++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 lcci/17.21.Volume of Histogram/Solution.swift 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.. Date: Mon, 13 May 2024 08:08:19 +0100 Subject: [PATCH 2/3] Swift Implementation for LCCI 17.21 --- lcci/17.21.Volume of Histogram/README.md | 32 +++++++++++++++++++++ lcci/17.21.Volume of Histogram/README_EN.md | 32 +++++++++++++++++++++ 2 files changed, 64 insertions(+) diff --git a/lcci/17.21.Volume of Histogram/README.md b/lcci/17.21.Volume of Histogram/README.md index f7efe350feec0..ec507a732b72d 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..8541de815a9ad 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.. From 4cfd41262a91cd276c8402188598a9296118a52a Mon Sep 17 00:00:00 2001 From: klever34 Date: Mon, 13 May 2024 07:09:37 +0000 Subject: [PATCH 3/3] style: format code and docs with prettier --- lcci/17.21.Volume of Histogram/README.md | 6 +++--- lcci/17.21.Volume of Histogram/README_EN.md | 6 +++--- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/lcci/17.21.Volume of Histogram/README.md b/lcci/17.21.Volume of Histogram/README.md index ec507a732b72d..7b4ffd2cfe962 100644 --- a/lcci/17.21.Volume of Histogram/README.md +++ b/lcci/17.21.Volume of Histogram/README.md @@ -163,10 +163,10 @@ class Solution { 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] @@ -182,7 +182,7 @@ class Solution { for i in 0..