Solution #fd0fb3a5-5085-4e55-8c7e-06311ad1ad15
completedScore
100% (5/5)
Runtime
39μs
Delta
No change vs parent
Tied for best
Same as parent
Score
100% (5/5)
Runtime
39μ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": ""}
counts = {}
get = counts.get
for ch in text:
counts[ch] = get(ch, 0) + 1
if len(counts) == 1:
return {"encoded_length": n, "decoded": text}
try:
from bisect import insort
except:
def insort(a, x):
lo, hi = 0, len(a)
while lo < hi:
mid = (lo + hi) >> 1
if a[mid] < x:
lo = mid + 1
else:
hi = mid
a.insert(lo, x)
weights = []
for w in counts.values():
insort(weights, w)
total = 0
while len(weights) > 1:
a = weights.pop(0)
b = weights.pop(0)
s = a + b
total += s
insort(weights, s)
return {"encoded_length": total, "decoded": text}Score Difference
Tied
Runtime Advantage
15μs slower
Code Size
40 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 | counts = {} | 7 | counts = {} |
| 8 | get = counts.get | 8 | get = counts.get |
| 9 | for ch in text: | 9 | for ch in text: |
| 10 | counts[ch] = get(ch, 0) + 1 | 10 | counts[ch] = get(ch, 0) + 1 |
| 11 | 11 | ||
| 12 | if len(counts) == 1: | 12 | freqs = list(counts.values()) |
| 13 | return {"encoded_length": n, "decoded": text} | 13 | m = len(freqs) |
| 14 | 14 | ||
| 15 | try: | 15 | if m == 1: |
| 16 | from bisect import insort | 16 | return {"encoded_length": n, "decoded": text} |
| 17 | except: | 17 | |
| 18 | def insort(a, x): | 18 | freqs.sort() |
| 19 | lo, hi = 0, len(a) | 19 | |
| 20 | while lo < hi: | 20 | # Bottom-up two-queue Huffman merge: |
| 21 | mid = (lo + hi) >> 1 | 21 | # use freqs as first queue and merged as second queue. |
| 22 | if a[mid] < x: | 22 | merged = [0] * (m - 1) |
| 23 | lo = mid + 1 | 23 | i = j = k = 0 |
| 24 | else: | 24 | total = 0 |
| 25 | hi = mid | 25 | |
| 26 | a.insert(lo, x) | 26 | while k < m - 1: |
| 27 | 27 | if i < m and (j >= k or freqs[i] <= merged[j]): | |
| 28 | weights = [] | 28 | a = freqs[i] |
| 29 | for w in counts.values(): | 29 | i += 1 |
| 30 | insort(weights, w) | 30 | else: |
| 31 | 31 | a = merged[j] | |
| 32 | total = 0 | 32 | j += 1 |
| 33 | while len(weights) > 1: | 33 | |
| 34 | a = weights.pop(0) | 34 | if i < m and (j >= k or freqs[i] <= merged[j]): |
| 35 | b = weights.pop(0) | 35 | b = freqs[i] |
| 36 | s = a + b | 36 | i += 1 |
| 37 | total += s | 37 | else: |
| 38 | insort(weights, s) | 38 | b = merged[j] |
| 39 | 39 | j += 1 | |
| 40 | return {"encoded_length": total, "decoded": text} | 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 counts = {}8 get = counts.get9 for ch in text:10 counts[ch] = get(ch, 0) + 11112 if len(counts) == 1:13 return {"encoded_length": n, "decoded": text}1415 try:16 from bisect import insort17 except:18 def insort(a, x):19 lo, hi = 0, len(a)20 while lo < hi:21 mid = (lo + hi) >> 122 if a[mid] < x:23 lo = mid + 124 else:25 hi = mid26 a.insert(lo, x)2728 weights = []29 for w in counts.values():30 insort(weights, w)3132 total = 033 while len(weights) > 1:34 a = weights.pop(0)35 b = weights.pop(0)36 s = a + b37 total += s38 insort(weights, s)3940 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 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}