Swift Solution - Why TLE?


  • 0
    L

    I created a solution in Swift that should be running with O(NMK + KL) and O(MN) space. I even compared it with accepted Java solution and it should be the equivalent in terms of Time Complexity, however, I keep receiving TLE before completing all the tests. Am I missing something? How can I improve my time?

    class Solution {
        func findMaxForm(_ strs: [String], _ m: Int, _ n: Int) -> Int {
            var dp = [[Int]](repeating: [Int](repeating: 0, count: n+1), count: m+1)
            
            var k = strs.count-1
            while k >= 0 {
                var count = findCount(strs[k])
                var i = m
                while i >= count.1{
                    var j = n
                    while j >= count.0{
                        dp[i][j] = max(1 + dp[i-count.1][j-count.0], dp[i][j])
                        j -= 1
                    } 
                    i -= 1
                }
                k -= 1
            }
            return dp[m][n]
        }
        func findCount(_ str : String) -> (Int, Int){
            var count = (0, 0)
            for char in str.characters{
                if char == "0"{
                    count.1 += 1
                } else {
                    count.0 += 1
                }
            }
            return count
        }
    }
    

  • 0

    @luke23 This is fixed.


Log in to reply
 

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