A/C Python solution, easy to understand, use dictionary, beat 8.64%


  • 0
    W

    class Solution(object):
    def kthSmallest(self, matrix, k):
    """
    :type matrix: List[List[int]]
    :type k: int
    :rtype: int
    """
    result = {}

        for row in matrix:
            #print "row = ", row
            
            for i in row:
                #print "i = ", i
                
                if i not in result:
                    result[i] = 1
                else:
                    result[i] += 1
                    
        #print "result = ", result
        dkeys = result.keys()
        dkeys.sort()
        #sorted(result)
        #result.sort(key=lambda x: x[0])
        #print "after sort, result = ", result
        
        curIdx = 0
        for key in dkeys:
            print key, result[key]
            curIdx += result[key]
            
            if k <=curIdx:
                
                #print "found ", key
                return key

Log in to reply
 

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