Solution #9bcd8c52-e05e-4028-a394-4e15a7047f97
completedScore
20% (1/5)
Runtime
289μs
Delta
-66.7% vs parent
-80.0% vs best
Regression from parent
Score
20% (1/5)
Runtime
289μs
Delta
-66.7% vs parent
-80.0% vs best
Regression from parent
def solve(input):
text = input.get("text", "")
n = len(text)
if n == 0:
return {"encoded_length": 0, "decoded": ""}
counts = {}
_ = [counts.__setitem__(ch, counts.get(ch, 0) + 1) for ch in text]
vals = sorted(counts.values())
m = len(vals)
if m == 1:
return {"encoded_length": n, "decoded": text}
q = vals + [0] * (m - 1)
i = 0
j = m
end = m
def take():
nonlocal i, j, end
if i < m and (j >= end or q[i] <= q[j]):
v = q[i]
i += 1
return v
v = q[j]
j += 1
return v
total = 0
_ = [q.__setitem__(end + k, (lambda a, b: (total := total + a + b, a + b)[1])(take(), take())) for k in range(m - 1)]
return {"encoded_length": total, "decoded": text}Score Difference
-80.0%
Runtime Advantage
265μs slower
Code Size
33 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 | _ = [counts.__setitem__(ch, counts.get(ch, 0) + 1) for ch in text] | 8 | get = counts.get |
| 9 | vals = sorted(counts.values()) | 9 | for ch in text: |
| 10 | m = len(vals) | 10 | counts[ch] = get(ch, 0) + 1 |
| 11 | 11 | ||
| 12 | if m == 1: | 12 | freqs = list(counts.values()) |
| 13 | return {"encoded_length": n, "decoded": text} | 13 | m = len(freqs) |
| 14 | 14 | ||
| 15 | q = vals + [0] * (m - 1) | 15 | if m == 1: |
| 16 | i = 0 | 16 | return {"encoded_length": n, "decoded": text} |
| 17 | j = m | 17 | |
| 18 | end = m | 18 | freqs.sort() |
| 19 | 19 | ||
| 20 | def take(): | 20 | # Bottom-up two-queue Huffman merge: |
| 21 | nonlocal i, j, end | 21 | # use freqs as first queue and merged as second queue. |
| 22 | if i < m and (j >= end or q[i] <= q[j]): | 22 | merged = [0] * (m - 1) |
| 23 | v = q[i] | 23 | i = j = k = 0 |
| 24 | i += 1 | 24 | total = 0 |
| 25 | return v | 25 | |
| 26 | v = q[j] | 26 | while k < m - 1: |
| 27 | j += 1 | 27 | if i < m and (j >= k or freqs[i] <= merged[j]): |
| 28 | return v | 28 | a = freqs[i] |
| 29 | 29 | i += 1 | |
| 30 | total = 0 | 30 | else: |
| 31 | _ = [q.__setitem__(end + k, (lambda a, b: (total := total + a + b, a + b)[1])(take(), take())) for k in range(m - 1)] | 31 | a = merged[j] |
| 32 | 32 | j += 1 | |
| 33 | return {"encoded_length": total, "decoded": text} | 33 | |
| 34 | 34 | if i < m and (j >= k or freqs[i] <= merged[j]): | |
| 35 | 35 | b = freqs[i] | |
| 36 | 36 | i += 1 | |
| 37 | 37 | else: | |
| 38 | 38 | b = merged[j] | |
| 39 | 39 | j += 1 | |
| 40 | 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 _ = [counts.__setitem__(ch, counts.get(ch, 0) + 1) for ch in text]9 vals = sorted(counts.values())10 m = len(vals)1112 if m == 1:13 return {"encoded_length": n, "decoded": text}1415 q = vals + [0] * (m - 1)16 i = 017 j = m18 end = m1920 def take():21 nonlocal i, j, end22 if i < m and (j >= end or q[i] <= q[j]):23 v = q[i]24 i += 125 return v26 v = q[j]27 j += 128 return v2930 total = 031 _ = [q.__setitem__(end + k, (lambda a, b: (total := total + a + b, a + b)[1])(take(), take())) for k in range(m - 1)]3233 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}