Solution #e2a7405b-9c9b-49ea-a6a8-682f98d1e30a
completedScore
100% (5/5)
Runtime
50μs
Delta
No change vs parent
Tied for best
Same as parent
Score
100% (5/5)
Runtime
50μs
Delta
No change vs parent
Tied for best
Same as parent
def solve(input):
text = input.get("text", "")
n = len(text)
if not n:
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}
s = " ".join(map(str, counts.values()))
vals = [int(x) for x in s.split()]
vals.sort()
m = len(vals)
q = vals + [0] * (m - 1)
i = 0
j = m
end = m
total = 0
while end < 2 * 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
26μs slower
Code Size
46 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 not n: | 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 | s = " ".join(map(str, counts.values())) | 15 | if m == 1: |
| 16 | vals = [int(x) for x in s.split()] | 16 | return {"encoded_length": n, "decoded": text} |
| 17 | vals.sort() | 17 | |
| 18 | 18 | freqs.sort() | |
| 19 | m = len(vals) | 19 | |
| 20 | q = vals + [0] * (m - 1) | 20 | # Bottom-up two-queue Huffman merge: |
| 21 | i = 0 | 21 | # use freqs as first queue and merged as second queue. |
| 22 | j = m | 22 | merged = [0] * (m - 1) |
| 23 | end = m | 23 | i = j = k = 0 |
| 24 | total = 0 | 24 | total = 0 |
| 25 | 25 | ||
| 26 | while end < 2 * m - 1: | 26 | while k < m - 1: |
| 27 | if i < m and (j >= end or q[i] <= q[j]): | 27 | if i < m and (j >= k or freqs[i] <= merged[j]): |
| 28 | a = q[i] | 28 | a = freqs[i] |
| 29 | i += 1 | 29 | i += 1 |
| 30 | else: | 30 | else: |
| 31 | a = q[j] | 31 | a = merged[j] |
| 32 | j += 1 | 32 | j += 1 |
| 33 | 33 | ||
| 34 | if i < m and (j >= end or q[i] <= q[j]): | 34 | if i < m and (j >= k or freqs[i] <= merged[j]): |
| 35 | b = q[i] | 35 | b = freqs[i] |
| 36 | i += 1 | 36 | i += 1 |
| 37 | else: | 37 | else: |
| 38 | b = q[j] | 38 | b = merged[j] |
| 39 | j += 1 | 39 | j += 1 |
| 40 | 40 | ||
| 41 | s = a + b | 41 | s = a + b |
| 42 | q[end] = s | 42 | merged[k] = s |
| 43 | end += 1 | 43 | total += s |
| 44 | total += s | 44 | k += 1 |
| 45 | 45 | ||
| 46 | return {"encoded_length": total, "decoded": text} | 46 | return {"encoded_length": total, "decoded": text} |
1def solve(input):2 text = input.get("text", "")3 n = len(text)4 if not n: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 s = " ".join(map(str, counts.values()))16 vals = [int(x) for x in s.split()]17 vals.sort()1819 m = len(vals)20 q = vals + [0] * (m - 1)21 i = 022 j = m23 end = m24 total = 02526 while end < 2 * m - 1:27 if i < m and (j >= end or q[i] <= q[j]):28 a = q[i]29 i += 130 else:31 a = q[j]32 j += 13334 if i < m and (j >= end or q[i] <= q[j]):35 b = q[i]36 i += 137 else:38 b = q[j]39 j += 14041 s = a + b42 q[end] = s43 end += 144 total += s4546 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}