Why TLE?


  • 0
    T
    class Solution(object):
        def kthSmallest(self, matrix, k):
            """
            :type matrix: List[List[int]]
            :type k: int
            :rtype: int
            """
            
            return self.matrixRecur(matrix, (0, 0), (len(matrix), len(matrix[0])), k)
                
        def matrixRecur(self, splicedMatrix, left, right, k):
            start_row, start_col = left
            end_row, end_col = right
            mid_row = (start_row + end_row)/2
            mid_col = (start_col + end_col)/2
            
            i = start_row
            j = start_col
            lcount = 0
            while(not (i == mid_row and j == mid_col)):
                if j < len(splicedMatrix[0]):
                    j += 1
                else:
                    j = 0
                    i += 1
                lcount += 1
            if k == lcount:
               return splicedMatrix[mid_row][mid_col]
            elif k < lcount:
                return self.matrixRecur(splicedMatrix, (start_row, start_col), (mid_row, mid_col - 1), k)
            else:
                return self.matrixRecur(splicedMatrix, (mid_row, mid_col + 1), (end_row, end_col), k-lcount)
            
    

    I am implementing binary search. I don't understand why my solution times out. Can someone help me figure this out?


Log in to reply
 

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