https://jinblack.com/leetcode-swift/

DP solution using swift

class Solution {
    func maximalRectangle(_ matrix: [[Character]]) -> Int {
        guard !matrix.isEmpty else { return 0 }
        let m = matrix.count
        let n = matrix[0].count
        var left = Array<Int>(repeating: 0, count: n)
        var right = Array<Int>(repeating: n, count: n)
        var height = Array<Int>(repeating: 0, count: n)
        var maxA = 0
        for i in 0..<m {
            var curLeft = 0, curRight = n
            for j in 0..<n {
                if matrix[i][j] == "1" {
                    height[j] += 1
                    left[j] = max(left[j], curLeft)
                } else {
                    height[j] = 0
                    left[j] = 0
                    curLeft = j+1
                }
                if matrix[i][n-1-j] == "1" {
                    right[n-1-j] = min(right[n-1-j], curRight)
                } else {
                    right[n-j-1] = n
                    curRight = n-1-j
                }
            }
            for j in 0..<n {
                maxA = max(maxA, (right[j] - left[j])*height[j])
            }
        }
        return maxA
    }
}

Runtime: 340 ms, faster than 100.00% of Swift online submissions for Maximal Rectangle.
Memory Usage: 20.1 MB, less than 100.00% of Swift online submissions for Maximal Rectangle.