Swift Approach
Complexity

Time complexity:
O(n)


Space complexity:
O(n2)

Code
# Complexity
- Time complexity:
$$O(n)$$

- Space complexity:
$$O(n^2)$$

# Code
```swift []
class Solution {
    func minCost(_ n: Int, _ cost: [[Int]]) -> Int {
        // [[2,1,1], [99,1,99], [99,99,1]]
        var dp: [[[Int64]]] = Array(repeating: Array(repeating: [Int64](repeating: -1, count: 4), count: 4), count: n / 2 + 1)
        func recur(_ l: Int, _ r: Int, _ i: Int) -> Int64 {
            if i >= n/2 {
                return 0
            }
            if dp[i][l][r] != -1 {
                return dp[i][l][r]
            }
            var left = (0..<3).filter { $0 != l }
            var right = (0..<3).filter { $0 != r }
            var ans = Int64.max
            for h in left {
                for j in right {
                    if h == j {
                        continue
                    }
                    var cl = cost[i][h]
                    var cr = cost[n-i-1][j]
                    ans = min(ans, Int64(cl) + Int64(cr) + recur(h, j, i+1))
                }
            }
            dp[i][l][r] = ans
            return ans
        }
        return Int(recur(3, 3, 0))
    }
}
```
['Swift']