Solution #f68f5746-1281-447a-910e-83fc1d850202
completedScore
100% (5/5)
Runtime
85μs
Delta
No change vs parent
Tied for best
Same as parent
Score
100% (5/5)
Runtime
85μs
Delta
No change vs parent
Tied for best
Same as parent
def solve(input):
text = input.get("text", "")
n = len(text)
if n == 0:
return {"encoded_length": 0, "decoded": ""}
# Precomputed popcount table for wildcard direct-indexing requirement
POP8 = (
0,1,1,2,1,2,2,3,1,2,2,3,2,3,3,4,
1,2,2,3,2,3,3,4,2,3,3,4,3,4,4,5,
1,2,2,3,2,3,3,4,2,3,3,4,3,4,4,5,
2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6,
1,2,2,3,2,3,3,4,2,3,3,4,3,4,4,5,
2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6,
2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6,
3,4,4,5,4,5,5,6,4,5,5,6,5,6,6,7,
1,2,2,3,2,3,3,4,2,3,3,4,3,4,4,5,
2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6,
2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6,
3,4,4,5,4,5,5,6,4,5,5,6,5,6,6,7,
2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6,
3,4,4,5,4,5,5,6,4,5,5,6,5,6,6,7,
3,4,4,5,4,5,5,6,4,5,5,6,5,6,6,7,
4,5,5,6,5,6,6,7,5,6,6,7,6,7,7,8
)
# Radically different frequency counting:
# bitset per byte value using Python big ints, then direct-index popcount table.
try:
b = text.encode("latin1")
masks = [0] * 256
bit = 1
for c in b:
masks[c] |= bit
bit <<= 1
freqs = []
append = freqs.append
uniq = 0
for m in masks:
if m:
uniq += 1
cnt = 0
while m:
cnt += POP8[m & 255]
m >>= 8
append(cnt)
except:
counts = {}
get = counts.get
for ch in text:
counts[ch] = get(ch, 0) + 1
freqs = list(counts.values())
uniq = len(freqs)
if uniq <= 2:
return {"encoded_length": n, "decoded": text}
# Two-queue Huffman after sorting frequencies, avoiding heap operations.
freqs.sort()
m = len(freqs)
q2 = [0] * (m - 1)
i = j = k = 0
total = 0
def take():
nonlocal i, j
if i < m and (j >= k or freqs[i] <= q2[j]):
v = freqs[i]
i += 1
return v
v = q2[j]
j += 1
return v
while k < m - 1:
s = take() + take()
total += s
q2[k] = s
k += 1
return {"encoded_length": total, "decoded": text}Score Difference
Tied
Runtime Advantage
61μs slower
Code Size
82 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 | # Precomputed popcount table for wildcard direct-indexing requirement | 7 | counts = {} |
| 8 | POP8 = ( | 8 | get = counts.get |
| 9 | 0,1,1,2,1,2,2,3,1,2,2,3,2,3,3,4, | 9 | for ch in text: |
| 10 | 1,2,2,3,2,3,3,4,2,3,3,4,3,4,4,5, | 10 | counts[ch] = get(ch, 0) + 1 |
| 11 | 1,2,2,3,2,3,3,4,2,3,3,4,3,4,4,5, | 11 | |
| 12 | 2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6, | 12 | freqs = list(counts.values()) |
| 13 | 1,2,2,3,2,3,3,4,2,3,3,4,3,4,4,5, | 13 | m = len(freqs) |
| 14 | 2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6, | 14 | |
| 15 | 2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6, | 15 | if m == 1: |
| 16 | 3,4,4,5,4,5,5,6,4,5,5,6,5,6,6,7, | 16 | return {"encoded_length": n, "decoded": text} |
| 17 | 1,2,2,3,2,3,3,4,2,3,3,4,3,4,4,5, | 17 | |
| 18 | 2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6, | 18 | freqs.sort() |
| 19 | 2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6, | 19 | |
| 20 | 3,4,4,5,4,5,5,6,4,5,5,6,5,6,6,7, | 20 | # Bottom-up two-queue Huffman merge: |
| 21 | 2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6, | 21 | # use freqs as first queue and merged as second queue. |
| 22 | 3,4,4,5,4,5,5,6,4,5,5,6,5,6,6,7, | 22 | merged = [0] * (m - 1) |
| 23 | 3,4,4,5,4,5,5,6,4,5,5,6,5,6,6,7, | 23 | i = j = k = 0 |
| 24 | 4,5,5,6,5,6,6,7,5,6,6,7,6,7,7,8 | 24 | total = 0 |
| 25 | ) | 25 | |
| 26 | 26 | while k < m - 1: | |
| 27 | # Radically different frequency counting: | 27 | if i < m and (j >= k or freqs[i] <= merged[j]): |
| 28 | # bitset per byte value using Python big ints, then direct-index popcount table. | 28 | a = freqs[i] |
| 29 | try: | 29 | i += 1 |
| 30 | b = text.encode("latin1") | 30 | else: |
| 31 | masks = [0] * 256 | 31 | a = merged[j] |
| 32 | bit = 1 | 32 | j += 1 |
| 33 | for c in b: | 33 | |
| 34 | masks[c] |= bit | 34 | if i < m and (j >= k or freqs[i] <= merged[j]): |
| 35 | bit <<= 1 | 35 | b = freqs[i] |
| 36 | 36 | i += 1 | |
| 37 | freqs = [] | 37 | else: |
| 38 | append = freqs.append | 38 | b = merged[j] |
| 39 | uniq = 0 | 39 | j += 1 |
| 40 | for m in masks: | 40 | |
| 41 | if m: | 41 | s = a + b |
| 42 | uniq += 1 | 42 | merged[k] = s |
| 43 | cnt = 0 | 43 | total += s |
| 44 | while m: | 44 | k += 1 |
| 45 | cnt += POP8[m & 255] | 45 | |
| 46 | m >>= 8 | 46 | return {"encoded_length": total, "decoded": text} |
| 47 | append(cnt) | 47 | |
| 48 | except: | 48 | |
| 49 | counts = {} | 49 | |
| 50 | get = counts.get | 50 | |
| 51 | for ch in text: | 51 | |
| 52 | counts[ch] = get(ch, 0) + 1 | 52 | |
| 53 | freqs = list(counts.values()) | 53 | |
| 54 | uniq = len(freqs) | 54 | |
| 55 | 55 | ||
| 56 | if uniq <= 2: | 56 | |
| 57 | return {"encoded_length": n, "decoded": text} | 57 | |
| 58 | 58 | ||
| 59 | # Two-queue Huffman after sorting frequencies, avoiding heap operations. | 59 | |
| 60 | freqs.sort() | 60 | |
| 61 | m = len(freqs) | 61 | |
| 62 | q2 = [0] * (m - 1) | 62 | |
| 63 | i = j = k = 0 | 63 | |
| 64 | total = 0 | 64 | |
| 65 | 65 | ||
| 66 | def take(): | 66 | |
| 67 | nonlocal i, j | 67 | |
| 68 | if i < m and (j >= k or freqs[i] <= q2[j]): | 68 | |
| 69 | v = freqs[i] | 69 | |
| 70 | i += 1 | 70 | |
| 71 | return v | 71 | |
| 72 | v = q2[j] | 72 | |
| 73 | j += 1 | 73 | |
| 74 | return v | 74 | |
| 75 | 75 | ||
| 76 | while k < m - 1: | 76 | |
| 77 | s = take() + take() | 77 | |
| 78 | total += s | 78 | |
| 79 | q2[k] = s | 79 | |
| 80 | k += 1 | 80 | |
| 81 | 81 | ||
| 82 | return {"encoded_length": total, "decoded": text} | 82 |
1def solve(input):2 text = input.get("text", "")3 n = len(text)4 if n == 0:5 return {"encoded_length": 0, "decoded": ""}67 # Precomputed popcount table for wildcard direct-indexing requirement8 POP8 = (9 0,1,1,2,1,2,2,3,1,2,2,3,2,3,3,4,10 1,2,2,3,2,3,3,4,2,3,3,4,3,4,4,5,11 1,2,2,3,2,3,3,4,2,3,3,4,3,4,4,5,12 2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6,13 1,2,2,3,2,3,3,4,2,3,3,4,3,4,4,5,14 2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6,15 2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6,16 3,4,4,5,4,5,5,6,4,5,5,6,5,6,6,7,17 1,2,2,3,2,3,3,4,2,3,3,4,3,4,4,5,18 2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6,19 2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6,20 3,4,4,5,4,5,5,6,4,5,5,6,5,6,6,7,21 2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6,22 3,4,4,5,4,5,5,6,4,5,5,6,5,6,6,7,23 3,4,4,5,4,5,5,6,4,5,5,6,5,6,6,7,24 4,5,5,6,5,6,6,7,5,6,6,7,6,7,7,825 )2627 # Radically different frequency counting:28 # bitset per byte value using Python big ints, then direct-index popcount table.29 try:30 b = text.encode("latin1")31 masks = [0] * 25632 bit = 133 for c in b:34 masks[c] |= bit35 bit <<= 13637 freqs = []38 append = freqs.append39 uniq = 040 for m in masks:41 if m:42 uniq += 143 cnt = 044 while m:45 cnt += POP8[m & 255]46 m >>= 847 append(cnt)48 except:49 counts = {}50 get = counts.get51 for ch in text:52 counts[ch] = get(ch, 0) + 153 freqs = list(counts.values())54 uniq = len(freqs)5556 if uniq <= 2:57 return {"encoded_length": n, "decoded": text}5859 # Two-queue Huffman after sorting frequencies, avoiding heap operations.60 freqs.sort()61 m = len(freqs)62 q2 = [0] * (m - 1)63 i = j = k = 064 total = 06566 def take():67 nonlocal i, j68 if i < m and (j >= k or freqs[i] <= q2[j]):69 v = freqs[i]70 i += 171 return v72 v = q2[j]73 j += 174 return v7576 while k < m - 1:77 s = take() + take()78 total += s79 q2[k] = s80 k += 18182 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}