Compile time limit exceeded.


  • 0
    A

    I don't understand why it keeps telling my code below Compile time limit exceeded. Even if I have no problem running it on Xcode playground.

    class Solution {
        func minimumTotal(_ triangle: [[Int]]) -> Int {
            if triangle.count > 0 && triangle[0].count > 0{
                
                var ans = triangle//[[Int]]()
                var length = triangle.count
                
                //Initialize the last row
                for i in 0...length - 1{
                    ans[length - 1][i] = triangle[length - 1][i]
                }
                
                for i in stride(from: length - 2, through: 0, by: -1){
                    for j in stride(from: 0, through: i, by: 1){
                        ans[i][j] = min(ans[i + 1][j],ans[i + 1][j + 1]) + triangle[i][j]
                    }
                }
                
                return ans[0][0]
                
            }
            return -1
        }
    }
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.