Solution #6126deee-8594-4f76-8ce0-72d8845a7ba5
completedScore
100% (5/5)
Runtime
51μs
Delta
No change vs parent
Tied for best
Same as parent
Score
100% (5/5)
Runtime
51μ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 = {}
[counts.__setitem__(ch, counts.get(ch, 0) + 1) for ch in text]
vals = list(counts.values())
if len(vals) == 1:
return {"encoded_length": n, "decoded": text}
vals.sort()
q = vals
m = len(q)
head1 = 0
head2 = m
q.extend([0] * (m - 1))
tail = m
total = 0
def popmin():
nonlocal head1, head2
if head1 < m and (head2 >= tail or q[head1] <= q[head2]):
v = q[head1]
head1 += 1
return v
v = q[head2]
head2 += 1
return v
while tail < 2 * m - 1:
s = popmin() + popmin()
q[tail] = s
tail += 1
total += s
return {"encoded_length": total, "decoded": text}Score Difference
Tied
Runtime Advantage
27μs slower
Code Size
39 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 | [counts.__setitem__(ch, counts.get(ch, 0) + 1) for ch in text] | 8 | get = counts.get |
| 9 | vals = list(counts.values()) | 9 | for ch in text: |
| 10 | 10 | counts[ch] = get(ch, 0) + 1 | |
| 11 | if len(vals) == 1: | 11 | |
| 12 | return {"encoded_length": n, "decoded": text} | 12 | freqs = list(counts.values()) |
| 13 | 13 | m = len(freqs) | |
| 14 | vals.sort() | 14 | |
| 15 | q = vals | 15 | if m == 1: |
| 16 | m = len(q) | 16 | return {"encoded_length": n, "decoded": text} |
| 17 | head1 = 0 | 17 | |
| 18 | head2 = m | 18 | freqs.sort() |
| 19 | q.extend([0] * (m - 1)) | 19 | |
| 20 | tail = m | 20 | # Bottom-up two-queue Huffman merge: |
| 21 | total = 0 | 21 | # use freqs as first queue and merged as second queue. |
| 22 | 22 | merged = [0] * (m - 1) | |
| 23 | def popmin(): | 23 | i = j = k = 0 |
| 24 | nonlocal head1, head2 | 24 | total = 0 |
| 25 | if head1 < m and (head2 >= tail or q[head1] <= q[head2]): | 25 | |
| 26 | v = q[head1] | 26 | while k < m - 1: |
| 27 | head1 += 1 | 27 | if i < m and (j >= k or freqs[i] <= merged[j]): |
| 28 | return v | 28 | a = freqs[i] |
| 29 | v = q[head2] | 29 | i += 1 |
| 30 | head2 += 1 | 30 | else: |
| 31 | return v | 31 | a = merged[j] |
| 32 | 32 | j += 1 | |
| 33 | while tail < 2 * m - 1: | 33 | |
| 34 | s = popmin() + popmin() | 34 | if i < m and (j >= k or freqs[i] <= merged[j]): |
| 35 | q[tail] = s | 35 | b = freqs[i] |
| 36 | tail += 1 | 36 | i += 1 |
| 37 | total += s | 37 | else: |
| 38 | 38 | b = merged[j] | |
| 39 | return {"encoded_length": total, "decoded": text} | 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 not n:5 return {"encoded_length": 0, "decoded": ""}67 counts = {}8 [counts.__setitem__(ch, counts.get(ch, 0) + 1) for ch in text]9 vals = list(counts.values())1011 if len(vals) == 1:12 return {"encoded_length": n, "decoded": text}1314 vals.sort()15 q = vals16 m = len(q)17 head1 = 018 head2 = m19 q.extend([0] * (m - 1))20 tail = m21 total = 02223 def popmin():24 nonlocal head1, head225 if head1 < m and (head2 >= tail or q[head1] <= q[head2]):26 v = q[head1]27 head1 += 128 return v29 v = q[head2]30 head2 += 131 return v3233 while tail < 2 * m - 1:34 s = popmin() + popmin()35 q[tail] = s36 tail += 137 total += s3839 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}