Solution #1a8aec00-cefc-4202-a34b-60fbbe4e793b
completedScore
60% (3/5)
Runtime
20.00s
Delta
-40.0% vs parent
-40.0% vs best
Regression from parent
Score
60% (3/5)
Runtime
20.00s
Delta
-40.0% vs parent
-40.0% vs best
Regression from parent
def solve(input):
text = input.get("text", "")
if not text:
return {"encoded_length": 0, "decoded": ""}
counts = {}
_ = [counts.__setitem__(ch, counts.get(ch, 0) + 1) for ch in text]
vals = sorted(counts.values())
if len(vals) == 1:
return {"encoded_length": len(text), "decoded": text}
a = vals
b = []
i = j = total = 0
la = len(a)
while (la - i) + (len(b) - j) > 1:
x = a[i] if i < la and (j >= len(b) or a[i] <= b[j]) else b[j]
i += i < la and (j >= len(b) or a[i] <= b[j])
j += not (i <= la and (i and x == a[i - 1] and (j >= len(b) or x <= (b[j] if j < len(b) else x + 1))))
y = a[i] if i < la and (j >= len(b) or a[i] <= b[j]) else b[j]
i += i < la and (j >= len(b) or a[i] <= b[j])
j += not (i <= la and (i and y == a[i - 1] and (j >= len(b) or y <= (b[j] if j < len(b) else y + 1))))
s = x + y
b.append(s)
total += s
return {"encoded_length": total, "decoded": text}Score Difference
-40.0%
Runtime Advantage
20.00s slower
Code Size
28 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 | if not text: | 3 | n = len(text) |
| 4 | return {"encoded_length": 0, "decoded": ""} | 4 | if n == 0: |
| 5 | 5 | return {"encoded_length": 0, "decoded": ""} | |
| 6 | counts = {} | 6 | |
| 7 | _ = [counts.__setitem__(ch, counts.get(ch, 0) + 1) for ch in text] | 7 | counts = {} |
| 8 | vals = sorted(counts.values()) | 8 | get = counts.get |
| 9 | if len(vals) == 1: | 9 | for ch in text: |
| 10 | return {"encoded_length": len(text), "decoded": text} | 10 | counts[ch] = get(ch, 0) + 1 |
| 11 | 11 | ||
| 12 | a = vals | 12 | freqs = list(counts.values()) |
| 13 | b = [] | 13 | m = len(freqs) |
| 14 | i = j = total = 0 | 14 | |
| 15 | la = len(a) | 15 | if m == 1: |
| 16 | 16 | return {"encoded_length": n, "decoded": text} | |
| 17 | while (la - i) + (len(b) - j) > 1: | 17 | |
| 18 | x = a[i] if i < la and (j >= len(b) or a[i] <= b[j]) else b[j] | 18 | freqs.sort() |
| 19 | i += i < la and (j >= len(b) or a[i] <= b[j]) | 19 | |
| 20 | j += not (i <= la and (i and x == a[i - 1] and (j >= len(b) or x <= (b[j] if j < len(b) else x + 1)))) | 20 | # Bottom-up two-queue Huffman merge: |
| 21 | y = a[i] if i < la and (j >= len(b) or a[i] <= b[j]) else b[j] | 21 | # use freqs as first queue and merged as second queue. |
| 22 | i += i < la and (j >= len(b) or a[i] <= b[j]) | 22 | merged = [0] * (m - 1) |
| 23 | j += not (i <= la and (i and y == a[i - 1] and (j >= len(b) or y <= (b[j] if j < len(b) else y + 1)))) | 23 | i = j = k = 0 |
| 24 | s = x + y | 24 | total = 0 |
| 25 | b.append(s) | 25 | |
| 26 | total += s | 26 | while k < m - 1: |
| 27 | 27 | if i < m and (j >= k or freqs[i] <= merged[j]): | |
| 28 | return {"encoded_length": total, "decoded": text} | 28 | a = freqs[i] |
| 29 | 29 | i += 1 | |
| 30 | 30 | else: | |
| 31 | 31 | a = merged[j] | |
| 32 | 32 | j += 1 | |
| 33 | 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 if not text:4 return {"encoded_length": 0, "decoded": ""}56 counts = {}7 _ = [counts.__setitem__(ch, counts.get(ch, 0) + 1) for ch in text]8 vals = sorted(counts.values())9 if len(vals) == 1:10 return {"encoded_length": len(text), "decoded": text}1112 a = vals13 b = []14 i = j = total = 015 la = len(a)1617 while (la - i) + (len(b) - j) > 1:18 x = a[i] if i < la and (j >= len(b) or a[i] <= b[j]) else b[j]19 i += i < la and (j >= len(b) or a[i] <= b[j])20 j += not (i <= la and (i and x == a[i - 1] and (j >= len(b) or x <= (b[j] if j < len(b) else x + 1))))21 y = a[i] if i < la and (j >= len(b) or a[i] <= b[j]) else b[j]22 i += i < la and (j >= len(b) or a[i] <= b[j])23 j += not (i <= la and (i and y == a[i - 1] and (j >= len(b) or y <= (b[j] if j < len(b) else y + 1))))24 s = x + y25 b.append(s)26 total += s2728 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}