Solution #62c233ed-0a9d-4c6c-b42a-d898828da64d
completedScore
100% (5/5)
Runtime
55μs
Delta
No change vs parent
Tied for best
Same as parent
Score
100% (5/5)
Runtime
55μs
Delta
No change vs parent
Tied for best
Same as parent
def solve(input):
text = input.get("text", "")
n = len(text)
if n == 0:
return {"encoded_length": 0, "decoded": ""}
try:
vals = tuple(__import__("collections").Counter(text.encode("latin1")).values())
except:
vals = tuple(__import__("collections").Counter(text).values())
if len(vals) == 1:
return {"encoded_length": n, "decoded": text}
h = list(vals)
heapq = __import__("heapq")
heapq.heapify(h)
def merge(heap, total=0):
if len(heap) == 1:
return total
a = heapq.heappop(heap)
b = heapq.heappop(heap)
s = a + b
heapq.heappush(heap, s)
return merge(heap, total + s)
return {"encoded_length": merge(h), "decoded": text}Score Difference
Tied
Runtime Advantage
31μs slower
Code Size
28 vs 46 lines
| # | Your Solution | # | Champion |
|---|---|---|---|
| 1 | def solve(input): | 1 | def solve(input): |
| 2 | text = input.get("text", "") | 2 | text = input.get("text", "") |
| 3 | n = len(text) | 3 | n = len(text) |
| 4 | if n == 0: | 4 | if n == 0: |
| 5 | return {"encoded_length": 0, "decoded": ""} | 5 | return {"encoded_length": 0, "decoded": ""} |
| 6 | 6 | ||
| 7 | try: | 7 | counts = {} |
| 8 | vals = tuple(__import__("collections").Counter(text.encode("latin1")).values()) | 8 | get = counts.get |
| 9 | except: | 9 | for ch in text: |
| 10 | vals = tuple(__import__("collections").Counter(text).values()) | 10 | counts[ch] = get(ch, 0) + 1 |
| 11 | 11 | ||
| 12 | if len(vals) == 1: | 12 | freqs = list(counts.values()) |
| 13 | return {"encoded_length": n, "decoded": text} | 13 | m = len(freqs) |
| 14 | 14 | ||
| 15 | h = list(vals) | 15 | if m == 1: |
| 16 | heapq = __import__("heapq") | 16 | return {"encoded_length": n, "decoded": text} |
| 17 | heapq.heapify(h) | 17 | |
| 18 | 18 | freqs.sort() | |
| 19 | def merge(heap, total=0): | 19 | |
| 20 | if len(heap) == 1: | 20 | # Bottom-up two-queue Huffman merge: |
| 21 | return total | 21 | # use freqs as first queue and merged as second queue. |
| 22 | a = heapq.heappop(heap) | 22 | merged = [0] * (m - 1) |
| 23 | b = heapq.heappop(heap) | 23 | i = j = k = 0 |
| 24 | s = a + b | 24 | total = 0 |
| 25 | heapq.heappush(heap, s) | 25 | |
| 26 | return merge(heap, total + s) | 26 | while k < m - 1: |
| 27 | 27 | if i < m and (j >= k or freqs[i] <= merged[j]): | |
| 28 | return {"encoded_length": merge(h), "decoded": text} | 28 | a = freqs[i] |
| 29 | 29 | i += 1 | |
| 30 | 30 | else: | |
| 31 | 31 | a = merged[j] | |
| 32 | 32 | j += 1 | |
| 33 | 33 | ||
| 34 | 34 | if i < m and (j >= k or freqs[i] <= merged[j]): | |
| 35 | 35 | b = freqs[i] | |
| 36 | 36 | i += 1 | |
| 37 | 37 | else: | |
| 38 | 38 | b = merged[j] | |
| 39 | 39 | j += 1 | |
| 40 | 40 | ||
| 41 | 41 | s = a + b | |
| 42 | 42 | merged[k] = s | |
| 43 | 43 | total += s | |
| 44 | 44 | k += 1 | |
| 45 | 45 | ||
| 46 | 46 | return {"encoded_length": total, "decoded": text} |
1def solve(input):2 text = input.get("text", "")3 n = len(text)4 if n == 0:5 return {"encoded_length": 0, "decoded": ""}67 try:8 vals = tuple(__import__("collections").Counter(text.encode("latin1")).values())9 except:10 vals = tuple(__import__("collections").Counter(text).values())1112 if len(vals) == 1:13 return {"encoded_length": n, "decoded": text}1415 h = list(vals)16 heapq = __import__("heapq")17 heapq.heapify(h)1819 def merge(heap, total=0):20 if len(heap) == 1:21 return total22 a = heapq.heappop(heap)23 b = heapq.heappop(heap)24 s = a + b25 heapq.heappush(heap, s)26 return merge(heap, total + s)2728 return {"encoded_length": merge(h), "decoded": text}1def solve(input):2 text = input.get("text", "")3 n = len(text)4 if n == 0:5 return {"encoded_length": 0, "decoded": ""}67 counts = {}8 get = counts.get9 for ch in text:10 counts[ch] = get(ch, 0) + 11112 freqs = list(counts.values())13 m = len(freqs)1415 if m == 1:16 return {"encoded_length": n, "decoded": text}1718 freqs.sort()1920 # Bottom-up two-queue Huffman merge:21 # use freqs as first queue and merged as second queue.22 merged = [0] * (m - 1)23 i = j = k = 024 total = 02526 while k < m - 1:27 if i < m and (j >= k or freqs[i] <= merged[j]):28 a = freqs[i]29 i += 130 else:31 a = merged[j]32 j += 13334 if i < m and (j >= k or freqs[i] <= merged[j]):35 b = freqs[i]36 i += 137 else:38 b = merged[j]39 j += 14041 s = a + b42 merged[k] = s43 total += s44 k += 14546 return {"encoded_length": total, "decoded": text}