Solution #4b16e427-07d8-4ccb-9d87-fd1e02f4d97b
completedScore
60% (3/5)
Runtime
32μs
Delta
-40.0% vs parent
-40.0% vs best
Regression from parent
Score
60% (3/5)
Runtime
32μs
Delta
-40.0% vs parent
-40.0% vs best
Regression from parent
def solve(input):
text = input.get("text", "")
n = len(text)
if n == 0:
return {"encoded_length": 0, "decoded": ""}
if n == 1:
return {"encoded_length": 1, "decoded": text}
total = 0
# In-place frequency accumulation via sorted run-length encoding.
# This avoids a dict and builds the multiset of weights directly.
s = sorted(text)
# Single-symbol case handled via run counting
weights = []
prev = s[0]
cnt = 1
for i in range(1, n):
ch = s[i]
if ch == prev:
cnt += 1
else:
weights.append(cnt)
prev = ch
cnt = 1
weights.append(cnt)
m = len(weights)
if m == 1:
return {"encoded_length": n, "decoded": text}
# weights already sorted because equal chars are grouped in sorted(text)
# Two-queue Huffman merge without heap/list reinsertion.
merged = [0] * (m - 1)
i = 0
j = 0
k = 0
def take():
nonlocal i, j
if i < m and (j >= k or weights[i] <= merged[j]):
v = weights[i]
i += 1
return v
v = merged[j]
j += 1
return v
while k < m - 1:
a = take()
b = take()
s2 = a + b
total += s2
merged[k] = s2
k += 1
return {"encoded_length": total, "decoded": text}Score Difference
-40.0%
Runtime Advantage
8μs slower
Code Size
58 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 | if n == 1: | 7 | counts = {} |
| 8 | return {"encoded_length": 1, "decoded": text} | 8 | get = counts.get |
| 9 | 9 | for ch in text: | |
| 10 | total = 0 | 10 | counts[ch] = get(ch, 0) + 1 |
| 11 | # In-place frequency accumulation via sorted run-length encoding. | 11 | |
| 12 | # This avoids a dict and builds the multiset of weights directly. | 12 | freqs = list(counts.values()) |
| 13 | s = sorted(text) | 13 | m = len(freqs) |
| 14 | 14 | ||
| 15 | # Single-symbol case handled via run counting | 15 | if m == 1: |
| 16 | weights = [] | 16 | return {"encoded_length": n, "decoded": text} |
| 17 | prev = s[0] | 17 | |
| 18 | cnt = 1 | 18 | freqs.sort() |
| 19 | for i in range(1, n): | 19 | |
| 20 | ch = s[i] | 20 | # Bottom-up two-queue Huffman merge: |
| 21 | if ch == prev: | 21 | # use freqs as first queue and merged as second queue. |
| 22 | cnt += 1 | 22 | merged = [0] * (m - 1) |
| 23 | else: | 23 | i = j = k = 0 |
| 24 | weights.append(cnt) | 24 | total = 0 |
| 25 | prev = ch | 25 | |
| 26 | cnt = 1 | 26 | while k < m - 1: |
| 27 | weights.append(cnt) | 27 | if i < m and (j >= k or freqs[i] <= merged[j]): |
| 28 | 28 | a = freqs[i] | |
| 29 | m = len(weights) | 29 | i += 1 |
| 30 | if m == 1: | 30 | else: |
| 31 | return {"encoded_length": n, "decoded": text} | 31 | a = merged[j] |
| 32 | 32 | j += 1 | |
| 33 | # weights already sorted because equal chars are grouped in sorted(text) | 33 | |
| 34 | # Two-queue Huffman merge without heap/list reinsertion. | 34 | if i < m and (j >= k or freqs[i] <= merged[j]): |
| 35 | merged = [0] * (m - 1) | 35 | b = freqs[i] |
| 36 | i = 0 | 36 | i += 1 |
| 37 | j = 0 | 37 | else: |
| 38 | k = 0 | 38 | b = merged[j] |
| 39 | 39 | j += 1 | |
| 40 | def take(): | 40 | |
| 41 | nonlocal i, j | 41 | s = a + b |
| 42 | if i < m and (j >= k or weights[i] <= merged[j]): | 42 | merged[k] = s |
| 43 | v = weights[i] | 43 | total += s |
| 44 | i += 1 | 44 | k += 1 |
| 45 | return v | 45 | |
| 46 | v = merged[j] | 46 | return {"encoded_length": total, "decoded": text} |
| 47 | j += 1 | 47 | |
| 48 | return v | 48 | |
| 49 | 49 | ||
| 50 | while k < m - 1: | 50 | |
| 51 | a = take() | 51 | |
| 52 | b = take() | 52 | |
| 53 | s2 = a + b | 53 | |
| 54 | total += s2 | 54 | |
| 55 | merged[k] = s2 | 55 | |
| 56 | k += 1 | 56 | |
| 57 | 57 | ||
| 58 | return {"encoded_length": total, "decoded": text} | 58 |
1def solve(input):2 text = input.get("text", "")3 n = len(text)4 if n == 0:5 return {"encoded_length": 0, "decoded": ""}67 if n == 1:8 return {"encoded_length": 1, "decoded": text}910 total = 011 # In-place frequency accumulation via sorted run-length encoding.12 # This avoids a dict and builds the multiset of weights directly.13 s = sorted(text)1415 # Single-symbol case handled via run counting16 weights = []17 prev = s[0]18 cnt = 119 for i in range(1, n):20 ch = s[i]21 if ch == prev:22 cnt += 123 else:24 weights.append(cnt)25 prev = ch26 cnt = 127 weights.append(cnt)2829 m = len(weights)30 if m == 1:31 return {"encoded_length": n, "decoded": text}3233 # weights already sorted because equal chars are grouped in sorted(text)34 # Two-queue Huffman merge without heap/list reinsertion.35 merged = [0] * (m - 1)36 i = 037 j = 038 k = 03940 def take():41 nonlocal i, j42 if i < m and (j >= k or weights[i] <= merged[j]):43 v = weights[i]44 i += 145 return v46 v = merged[j]47 j += 148 return v4950 while k < m - 1:51 a = take()52 b = take()53 s2 = a + b54 total += s255 merged[k] = s256 k += 15758 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}