Skip to content

Commit 5587bc8

Browse files
Create reorder_data_in_log_files.py
1 parent 19f00bc commit 5587bc8

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed

Diff for: reorder_data_in_log_files.py

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
from heapq import heappush, heappop
2+
class Solution:
3+
def reorderLogFiles(self, logs: List[str]) -> List[str]:
4+
letterLogs, digitLogs, result = [], [], []
5+
6+
for log in logs:
7+
identifier, content = log.split(' ', 1)
8+
if content[0].isdigit():
9+
digitLogs.append(log)
10+
else:
11+
heappush(letterLogs, (content, identifier)) # heapify by lexicographical order of content and then identifier
12+
13+
while letterLogs:
14+
content, identifier = heapq.heappop(letterLogs) # lexicographically smallest element
15+
result.append(identifier + ' ' + content)
16+
17+
return result + digitLogs

0 commit comments

Comments
 (0)