Solution #9e71593e-ad2f-44f6-9247-8066511ef4ef
completedScore
100% (5/5)
Runtime
57μs
Delta
+150.0% vs parent
Tied for best
Improved from parent
Score
100% (5/5)
Runtime
57μs
Delta
+150.0% vs parent
Tied for best
Improved from parent
def solve(input):
text = input.get("text", "")
n = len(text)
if n == 0:
return {"encoded_length": 0, "decoded": ""}
try:
b = text.encode("utf-32-le")
counts = {}
get = counts.get
for i in range(0, len(b), 4):
cp = b[i] | (b[i + 1] << 8) | (b[i + 2] << 16) | (b[i + 3] << 24)
counts[cp] = get(cp, 0) + 1
except Exception:
counts = {}
get = counts.get
for ch in text:
counts[ch] = get(ch, 0) + 1
vals = list(counts.values())
if len(vals) == 1:
return {"encoded_length": n, "decoded": text}
vals.sort()
m = len(vals)
q = vals + [0] * (m - 1)
i = 0
j = m
end = m
total = 0
for _ in range(m - 1):
if i < m and (j >= end or q[i] <= q[j]):
a = q[i]
i += 1
else:
a = q[j]
j += 1
if i < m and (j >= end or q[i] <= q[j]):
b = q[i]
i += 1
else:
b = q[j]
j += 1
s = a + b
q[end] = s
end += 1
total += s
return {"encoded_length": total, "decoded": text}Score Difference
Tied
Runtime Advantage
33μs slower
Code Size
52 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 | b = text.encode("utf-32-le") | 8 | get = counts.get |
| 9 | counts = {} | 9 | for ch in text: |
| 10 | get = counts.get | 10 | counts[ch] = get(ch, 0) + 1 |
| 11 | for i in range(0, len(b), 4): | 11 | |
| 12 | cp = b[i] | (b[i + 1] << 8) | (b[i + 2] << 16) | (b[i + 3] << 24) | 12 | freqs = list(counts.values()) |
| 13 | counts[cp] = get(cp, 0) + 1 | 13 | m = len(freqs) |
| 14 | except Exception: | 14 | |
| 15 | counts = {} | 15 | if m == 1: |
| 16 | get = counts.get | 16 | return {"encoded_length": n, "decoded": text} |
| 17 | for ch in text: | 17 | |
| 18 | counts[ch] = get(ch, 0) + 1 | 18 | freqs.sort() |
| 19 | 19 | ||
| 20 | vals = list(counts.values()) | 20 | # Bottom-up two-queue Huffman merge: |
| 21 | if len(vals) == 1: | 21 | # use freqs as first queue and merged as second queue. |
| 22 | return {"encoded_length": n, "decoded": text} | 22 | merged = [0] * (m - 1) |
| 23 | 23 | i = j = k = 0 | |
| 24 | vals.sort() | 24 | total = 0 |
| 25 | m = len(vals) | 25 | |
| 26 | q = vals + [0] * (m - 1) | 26 | while k < m - 1: |
| 27 | i = 0 | 27 | if i < m and (j >= k or freqs[i] <= merged[j]): |
| 28 | j = m | 28 | a = freqs[i] |
| 29 | end = m | 29 | i += 1 |
| 30 | 30 | else: | |
| 31 | total = 0 | 31 | a = merged[j] |
| 32 | for _ in range(m - 1): | 32 | j += 1 |
| 33 | if i < m and (j >= end or q[i] <= q[j]): | 33 | |
| 34 | a = q[i] | 34 | if i < m and (j >= k or freqs[i] <= merged[j]): |
| 35 | i += 1 | 35 | b = freqs[i] |
| 36 | else: | 36 | i += 1 |
| 37 | a = q[j] | 37 | else: |
| 38 | j += 1 | 38 | b = merged[j] |
| 39 | 39 | j += 1 | |
| 40 | if i < m and (j >= end or q[i] <= q[j]): | 40 | |
| 41 | b = q[i] | 41 | s = a + b |
| 42 | i += 1 | 42 | merged[k] = s |
| 43 | else: | 43 | total += s |
| 44 | b = q[j] | 44 | k += 1 |
| 45 | j += 1 | 45 | |
| 46 | 46 | return {"encoded_length": total, "decoded": text} | |
| 47 | s = a + b | 47 | |
| 48 | q[end] = s | 48 | |
| 49 | end += 1 | 49 | |
| 50 | total += s | 50 | |
| 51 | 51 | ||
| 52 | return {"encoded_length": total, "decoded": text} | 52 |
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 b = text.encode("utf-32-le")9 counts = {}10 get = counts.get11 for i in range(0, len(b), 4):12 cp = b[i] | (b[i + 1] << 8) | (b[i + 2] << 16) | (b[i + 3] << 24)13 counts[cp] = get(cp, 0) + 114 except Exception:15 counts = {}16 get = counts.get17 for ch in text:18 counts[ch] = get(ch, 0) + 11920 vals = list(counts.values())21 if len(vals) == 1:22 return {"encoded_length": n, "decoded": text}2324 vals.sort()25 m = len(vals)26 q = vals + [0] * (m - 1)27 i = 028 j = m29 end = m3031 total = 032 for _ in range(m - 1):33 if i < m and (j >= end or q[i] <= q[j]):34 a = q[i]35 i += 136 else:37 a = q[j]38 j += 13940 if i < m and (j >= end or q[i] <= q[j]):41 b = q[i]42 i += 143 else:44 b = q[j]45 j += 14647 s = a + b48 q[end] = s49 end += 150 total += s5152 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}