LintCode 1380 – Python 3 (Week 09 – 05)

class Solution:
    """
    @param logs: the logs
    @return: the log after sorting
    """
    def logSort(self, logs):
        # Write your code here
        
        type1, type2 = [], []
        for log in logs:
            i = log.index(' ')
            if log[i+1].isdigit():
                type2.append(log)
            else:
                type1.append((i, log))
        
        type1.sort(key = lambda pair: (pair[1][pair[0]+1:], pair[1][:pair[0]]))
        output = [pair[1] for pair in type1]
        
        return output + type2

Time complexity is O(nlogn). Space complexity is O(1).

Leave a comment

Design a site like this with WordPress.com
Get started