Solution #f3599802-995a-41e2-acd1-e92a052ec12f
completedScore
100% (5/5)
Runtime
75μs
Delta
No change vs parent
Tied for best
Same as parent
Score
100% (5/5)
Runtime
75μ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": ""}
from array import array
counts = {}
get = counts.get
for ch in text:
counts[ch] = get(ch, 0) + 1
m = len(counts)
if m == 1:
return {"encoded_length": n, "decoded": text}
freqs = array('Q', counts.values())
freqs = array('Q', sorted(freqs))
q = array('Q', [0]) * (m - 1)
i = j = k = 0
total = 0
while k < m - 1:
if i < m and (j >= k or freqs[i] <= q[j]):
a = freqs[i]
i += 1
else:
a = q[j]
j += 1
if i < m and (j >= k or freqs[i] <= q[j]):
b = freqs[i]
i += 1
else:
b = q[j]
j += 1
s = a + b
q[k] = s
total += s
k += 1
return {"encoded_length": total, "decoded": text}Score Difference
Tied
Runtime Advantage
51μs slower
Code Size
45 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 | from array import array | 7 | counts = {} |
| 8 | 8 | get = counts.get | |
| 9 | counts = {} | 9 | for ch in text: |
| 10 | get = counts.get | 10 | counts[ch] = get(ch, 0) + 1 |
| 11 | for ch in text: | 11 | |
| 12 | counts[ch] = get(ch, 0) + 1 | 12 | freqs = list(counts.values()) |
| 13 | 13 | m = len(freqs) | |
| 14 | m = len(counts) | 14 | |
| 15 | if m == 1: | 15 | if m == 1: |
| 16 | return {"encoded_length": n, "decoded": text} | 16 | return {"encoded_length": n, "decoded": text} |
| 17 | 17 | ||
| 18 | freqs = array('Q', counts.values()) | 18 | freqs.sort() |
| 19 | freqs = array('Q', sorted(freqs)) | 19 | |
| 20 | 20 | # Bottom-up two-queue Huffman merge: | |
| 21 | q = array('Q', [0]) * (m - 1) | 21 | # use freqs as first queue and merged as second queue. |
| 22 | i = j = k = 0 | 22 | merged = [0] * (m - 1) |
| 23 | total = 0 | 23 | i = j = k = 0 |
| 24 | 24 | total = 0 | |
| 25 | while k < m - 1: | 25 | |
| 26 | if i < m and (j >= k or freqs[i] <= q[j]): | 26 | while k < m - 1: |
| 27 | a = freqs[i] | 27 | if i < m and (j >= k or freqs[i] <= merged[j]): |
| 28 | i += 1 | 28 | a = freqs[i] |
| 29 | else: | 29 | i += 1 |
| 30 | a = q[j] | 30 | else: |
| 31 | j += 1 | 31 | a = merged[j] |
| 32 | 32 | j += 1 | |
| 33 | if i < m and (j >= k or freqs[i] <= q[j]): | 33 | |
| 34 | b = freqs[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 | b = q[j] | 37 | else: |
| 38 | j += 1 | 38 | b = merged[j] |
| 39 | 39 | j += 1 | |
| 40 | s = a + b | 40 | |
| 41 | q[k] = s | 41 | s = a + b |
| 42 | total += s | 42 | merged[k] = s |
| 43 | k += 1 | 43 | total += s |
| 44 | 44 | k += 1 | |
| 45 | return {"encoded_length": total, "decoded": text} | 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 from array import array89 counts = {}10 get = counts.get11 for ch in text:12 counts[ch] = get(ch, 0) + 11314 m = len(counts)15 if m == 1:16 return {"encoded_length": n, "decoded": text}1718 freqs = array('Q', counts.values())19 freqs = array('Q', sorted(freqs))2021 q = array('Q', [0]) * (m - 1)22 i = j = k = 023 total = 02425 while k < m - 1:26 if i < m and (j >= k or freqs[i] <= q[j]):27 a = freqs[i]28 i += 129 else:30 a = q[j]31 j += 13233 if i < m and (j >= k or freqs[i] <= q[j]):34 b = freqs[i]35 i += 136 else:37 b = q[j]38 j += 13940 s = a + b41 q[k] = s42 total += s43 k += 14445 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}