937. Reorder Data in Log Files

You are given an array of logs. Each log is a space-delimited string of words, where the first word is the identifier.

There are two types of logs:

Letter-logs: All words (except the identifier) consist of lowercase English letters.
Digit-logs: All words (except the identifier) consist of digits.
Reorder these logs so that:

The letter-logs come before all digit-logs.
The letter-logs are sorted lexicographically by their contents. If their contents are the same, then sort them lexicographically by their identifiers.
The digit-logs maintain their relative ordering.
Return the final order of the logs.

Example 1:

Input: logs = ["dig1 8 1 5 1","let1 art can","dig2 3 6","let2 own kit dig","let3 art zero"]
Output: ["let1 art can","let3 art zero","let2 own kit dig","dig1 8 1 5 1","dig2 3 6"]
Explanation:
The letter-log contents are all different, so their ordering is "art can", "art zero", "own kit dig".
The digit-logs have a relative order of "dig1 8 1 5 1", "dig2 3 6".

Example 2:

Input: logs = ["a1 9 2 3 1","g1 act car","zo4 4 7","ab1 off key dog","a8 act zoo"]
Output: ["g1 act car","a8 act zoo","ab1 off key dog","a1 9 2 3 1","zo4 4 7"]
class Solution:
    def reorderLogFiles(self, logs: List[str]) -> List[str]:
        # It's a sorting problem
        #Rather than defining pairwise relationships among all elements in a collection, the order of the elements can also be defined with sorting keys.
        # it should be clear that all we need is to translate the rules we defined before into a tuple of keys.
        #concerning the order of logs:
        
        #1). The letter-logs should be prioritized above all digit-logs.
        #2). Among the letter-logs, we should further sort them based on firstly on their contents, and then on their identifiers if the contents are identical.
        #3). Among the digit-logs, they should remain in the same order as they are in the collection. -----sorting algorithms stability
        #define a tuple of 3 keys(key1, key2, key3) digit-log(1,none,none)
        #key1:for the letter-logs,key1 = 0; for the digit-logs,key1 = 1
        #key2:represent letter-logs content,digit-logs don't need key2,key3
        #key3:further order the letter-logs,use identifier of the letter-logs as its value
        def get_key(log):
            _id, rest = log.split(" ", maxsplit=1)
            return (0, rest, _id) if rest[0].isalpha() else (1, 0, 0)
        return sorted(logs,key = get_key)
        #N:number of logs; M:maximum length of a dingle log,
        #sorted:O(nlogn); key of logs:O(M)
        #time complexity: O(M * N * logN)
        #space complexity:O(M + N)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值