[ LeetCode #937 ]

[ LeetCode #937 ] Reorder Log Files 로그 파일 재정렬 #

[ LeetCode #937 ] Reorder Log Files 바로가기 #

💡 유용한 지식 #

리스트 정렬

# 리스트 정렬 index 반환
index = sorted(range(len(s)), key = lambda x : s[x])
s = [s[i] for i in index]

# string split list로 sort
s.sort(key = lambda x: (x.split(' ')[1:], x.split(' ')[0]))

# 함수를 이용한 sorting
def fn(s):
	return s[0], s[-1]
sorted(a, key=fn)
sorted(a, key=len)

문제 #

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:

  1. The letter-logs come before all digit-logs.
  2. The letter-logs are sorted lexicographically by their contents. If their contents are the same, then sort them lexicographically by their identifiers.
  3. The digit-logs maintain their relative ordering.

Return the final order of the logs.

Constraints:

  • 1 <= logs.length <= 100
  • 3 <= logs[i].length <= 100
  • All the tokens of logs[i] are separated by a single space.
  • logs[i] is guaranteed to have an identifier and at least one word after the identifier

내가 했던 접근 #

[ 풀이 ]

  1. log 맨 앞 identifier 다음 단어가 letter인지 digit 판별하여 각각 별도 리스트로 저장
  2. letter 인 리스트 sort하면서 index 를 반환받아서 letter log 재정렬
  3. 마지막 결과로 letter + digit 반환

[ 결과 ]

Runtime: 43 ms, faster than 29.75% of Python online submissions for Reorder Data in Log Files.
Memory Usage: 13.8 MB, less than 23.66% of Python online submissions for Reorder Data in Log Files.

[ 코드 ]

class Solution(object):
    def reorderLogFiles(self, logs):
        letter_logs = []
        digit_logs = []
        for log in logs:
            if log.split(' ')[1].isnumeric():
                digit_logs.append(log)
            else:
                letter_logs.append(log)

        sort_index = sorted(range(len(letter_logs)),
                            key = lambda x: (' '.join(letter_logs[x].split(' ')[1:]),
                                             letter_logs[x].split(' ')[0]))

        letter_logs = [letter_logs[i] for i in sort_index]
        return letter_logs + digit_logs

[ 반성 ]

  • 굳이 index, join()해서 string으로 변환할 필요 없음, list 그대로 sort 가능
## 굳이 index, join()해서 string으로 변환할 필요 X, list 그대로 sort 가능하다
sort_index = sorted(range(len(letter_logs)),
                            key = lambda x: (' '.join(letter_logs[x].split(' ')[1:]),
                                             letter_logs[x].split(' ')[0]))
letter_logs = [letter_logs[i] for i in sort_index]
## 이런식으로
letter_logs.sort(key = lambda x : (x.split(' ')[1:], x.split(' ')[0]))

책 풀이 #

[ 풀이 ]

  • 내 접근과 동일하지만 letter 정렬시 index가 아닌 직접 sort 함수 적용

[ 결과 ]

Runtime: 38 ms, faster than 44.80% of Python online submissions for Reorder Data in Log Files.
Memory Usage: 13.7 MB, less than 77.18% of Python online submissions for Reorder Data in Log Files.

[ 코드 ]

class Solution(object):
    def reorderLogFiles(self, logs):
        letter_logs = []
        digit_logs = []
        for log in logs:
            if log.split(' ')[1].isdigit():
                digit_logs.append(log)
            else:
                letter_logs.append(log)

        letter_logs.sort(key = lambda x : (x.split(' ')[1:], x.split(' ')[0]))
        return letter_logs + digit_logs