From 344d8078de1f83cf524881e4835a856f955ce1c3 Mon Sep 17 00:00:00 2001 From: Lanre Adedara Date: Thu, 6 Jun 2024 06:27:00 +0100 Subject: [PATCH] feat: add swift implementation to lcof2 problem: No.039 --- .../README.md" | 42 +++++++++++++++++++ .../Solution.swift" | 37 ++++++++++++++++ 2 files changed, 79 insertions(+) create mode 100644 "lcof2/\345\211\221\346\214\207 Offer II 039. \347\233\264\346\226\271\345\233\276\346\234\200\345\244\247\347\237\251\345\275\242\351\235\242\347\247\257/Solution.swift" diff --git "a/lcof2/\345\211\221\346\214\207 Offer II 039. \347\233\264\346\226\271\345\233\276\346\234\200\345\244\247\347\237\251\345\275\242\351\235\242\347\247\257/README.md" "b/lcof2/\345\211\221\346\214\207 Offer II 039. \347\233\264\346\226\271\345\233\276\346\234\200\345\244\247\347\237\251\345\275\242\351\235\242\347\247\257/README.md" index 0f92c4a6a385f..ed0922fce83b5 100644 --- "a/lcof2/\345\211\221\346\214\207 Offer II 039. \347\233\264\346\226\271\345\233\276\346\234\200\345\244\247\347\237\251\345\275\242\351\235\242\347\247\257/README.md" +++ "b/lcof2/\345\211\221\346\214\207 Offer II 039. \347\233\264\346\226\271\345\233\276\346\234\200\345\244\247\347\237\251\345\275\242\351\235\242\347\247\257/README.md" @@ -235,6 +235,48 @@ function largestRectangleArea(heights: number[]): number { } ``` +#### Swift + +```swift +class Solution { + func largestRectangleArea(_ heights: [Int]) -> Int { + let n = heights.count + var left = [Int](repeating: -1, count: n) + var right = [Int](repeating: n, count: n) + var stack = [Int]() + + for i in 0..= heights[i] { + stack.removeLast() + } + if !stack.isEmpty { + left[i] = stack.last! + } + stack.append(i) + } + + stack.removeAll() + + for i in stride(from: n - 1, through: 0, by: -1) { + while !stack.isEmpty && heights[stack.last!] >= heights[i] { + stack.removeLast() + } + if !stack.isEmpty { + right[i] = stack.last! + } + stack.append(i) + } + + var maxArea = 0 + for i in 0.. diff --git "a/lcof2/\345\211\221\346\214\207 Offer II 039. \347\233\264\346\226\271\345\233\276\346\234\200\345\244\247\347\237\251\345\275\242\351\235\242\347\247\257/Solution.swift" "b/lcof2/\345\211\221\346\214\207 Offer II 039. \347\233\264\346\226\271\345\233\276\346\234\200\345\244\247\347\237\251\345\275\242\351\235\242\347\247\257/Solution.swift" new file mode 100644 index 0000000000000..aa1548fc883fb --- /dev/null +++ "b/lcof2/\345\211\221\346\214\207 Offer II 039. \347\233\264\346\226\271\345\233\276\346\234\200\345\244\247\347\237\251\345\275\242\351\235\242\347\247\257/Solution.swift" @@ -0,0 +1,37 @@ +class Solution { + func largestRectangleArea(_ heights: [Int]) -> Int { + let n = heights.count + var left = [Int](repeating: -1, count: n) + var right = [Int](repeating: n, count: n) + var stack = [Int]() + + for i in 0..= heights[i] { + stack.removeLast() + } + if !stack.isEmpty { + left[i] = stack.last! + } + stack.append(i) + } + + stack.removeAll() + + for i in stride(from: n - 1, through: 0, by: -1) { + while !stack.isEmpty && heights[stack.last!] >= heights[i] { + stack.removeLast() + } + if !stack.isEmpty { + right[i] = stack.last! + } + stack.append(i) + } + + var maxArea = 0 + for i in 0..