Solution #195f1ac7-f7ec-44cc-a52b-40995e0968f1
completedScore
100% (5/5)
Runtime
82μs
Delta
No change vs parent
Tied for best
Same as parent
Score
100% (5/5)
Runtime
82μ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": ""}
counts = [0] * 256
ascii_only = True
unique = 0
for ch in text:
o = ord(ch)
if o < 256:
if counts[o] == 0:
unique += 1
counts[o] += 1
else:
ascii_only = False
break
if ascii_only:
if unique == 1:
return {"encoded_length": n, "decoded": text}
heap = [0] * unique
idx = 0
for c in counts:
if c:
heap[idx] = c
idx += 1
else:
d = {}
get = d.get
for ch in text:
d[ch] = get(ch, 0) + 1
if len(d) == 1:
return {"encoded_length": n, "decoded": text}
heap = [0] * len(d)
idx = 0
for c in d.values():
heap[idx] = c
idx += 1
size = len(heap)
i = (size >> 1) - 1
while i >= 0:
j = i
x = heap[j]
while True:
l = (j << 1) + 1
if l >= size:
break
r = l + 1
c = l
if r < size and heap[r] < heap[l]:
c = r
if heap[c] >= x:
break
heap[j] = heap[c]
j = c
heap[j] = x
i -= 1
total = 0
while size > 1:
a = heap[0]
size -= 1
x = heap[size]
j = 0
while True:
l = (j << 1) + 1
if l >= size:
break
r = l + 1
c = l
if r < size and heap[r] < heap[l]:
c = r
if heap[c] >= x:
break
heap[j] = heap[c]
j = c
if size:
heap[j] = x
b = heap[0]
size -= 1
x = heap[size] if size else 0
j = 0
while True:
l = (j << 1) + 1
if l >= size:
break
r = l + 1
c = l
if r < size and heap[r] < heap[l]:
c = r
if heap[c] >= x:
break
heap[j] = heap[c]
j = c
if size:
heap[j] = x
s = a + b
total += s
j = size
size += 1
while j:
p = (j - 1) >> 1
if heap[p] <= s:
break
heap[j] = heap[p]
j = p
if j < len(heap):
heap[j] = s
else:
heap.append(s)
return {"encoded_length": total, "decoded": text}Score Difference
Tied
Runtime Advantage
58μs slower
Code Size
122 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 = [0] * 256 | 7 | counts = {} |
| 8 | ascii_only = True | 8 | get = counts.get |
| 9 | unique = 0 | 9 | for ch in text: |
| 10 | 10 | counts[ch] = get(ch, 0) + 1 | |
| 11 | for ch in text: | 11 | |
| 12 | o = ord(ch) | 12 | freqs = list(counts.values()) |
| 13 | if o < 256: | 13 | m = len(freqs) |
| 14 | if counts[o] == 0: | 14 | |
| 15 | unique += 1 | 15 | if m == 1: |
| 16 | counts[o] += 1 | 16 | return {"encoded_length": n, "decoded": text} |
| 17 | else: | 17 | |
| 18 | ascii_only = False | 18 | freqs.sort() |
| 19 | break | 19 | |
| 20 | 20 | # Bottom-up two-queue Huffman merge: | |
| 21 | if ascii_only: | 21 | # use freqs as first queue and merged as second queue. |
| 22 | if unique == 1: | 22 | merged = [0] * (m - 1) |
| 23 | return {"encoded_length": n, "decoded": text} | 23 | i = j = k = 0 |
| 24 | 24 | total = 0 | |
| 25 | heap = [0] * unique | 25 | |
| 26 | idx = 0 | 26 | while k < m - 1: |
| 27 | for c in counts: | 27 | if i < m and (j >= k or freqs[i] <= merged[j]): |
| 28 | if c: | 28 | a = freqs[i] |
| 29 | heap[idx] = c | 29 | i += 1 |
| 30 | idx += 1 | 30 | else: |
| 31 | else: | 31 | a = merged[j] |
| 32 | d = {} | 32 | j += 1 |
| 33 | get = d.get | 33 | |
| 34 | for ch in text: | 34 | if i < m and (j >= k or freqs[i] <= merged[j]): |
| 35 | d[ch] = get(ch, 0) + 1 | 35 | b = freqs[i] |
| 36 | if len(d) == 1: | 36 | i += 1 |
| 37 | return {"encoded_length": n, "decoded": text} | 37 | else: |
| 38 | heap = [0] * len(d) | 38 | b = merged[j] |
| 39 | idx = 0 | 39 | j += 1 |
| 40 | for c in d.values(): | 40 | |
| 41 | heap[idx] = c | 41 | s = a + b |
| 42 | idx += 1 | 42 | merged[k] = s |
| 43 | 43 | total += s | |
| 44 | size = len(heap) | 44 | k += 1 |
| 45 | 45 | ||
| 46 | i = (size >> 1) - 1 | 46 | return {"encoded_length": total, "decoded": text} |
| 47 | while i >= 0: | 47 | |
| 48 | j = i | 48 | |
| 49 | x = heap[j] | 49 | |
| 50 | while True: | 50 | |
| 51 | l = (j << 1) + 1 | 51 | |
| 52 | if l >= size: | 52 | |
| 53 | break | 53 | |
| 54 | r = l + 1 | 54 | |
| 55 | c = l | 55 | |
| 56 | if r < size and heap[r] < heap[l]: | 56 | |
| 57 | c = r | 57 | |
| 58 | if heap[c] >= x: | 58 | |
| 59 | break | 59 | |
| 60 | heap[j] = heap[c] | 60 | |
| 61 | j = c | 61 | |
| 62 | heap[j] = x | 62 | |
| 63 | i -= 1 | 63 | |
| 64 | 64 | ||
| 65 | total = 0 | 65 | |
| 66 | 66 | ||
| 67 | while size > 1: | 67 | |
| 68 | a = heap[0] | 68 | |
| 69 | size -= 1 | 69 | |
| 70 | x = heap[size] | 70 | |
| 71 | j = 0 | 71 | |
| 72 | while True: | 72 | |
| 73 | l = (j << 1) + 1 | 73 | |
| 74 | if l >= size: | 74 | |
| 75 | break | 75 | |
| 76 | r = l + 1 | 76 | |
| 77 | c = l | 77 | |
| 78 | if r < size and heap[r] < heap[l]: | 78 | |
| 79 | c = r | 79 | |
| 80 | if heap[c] >= x: | 80 | |
| 81 | break | 81 | |
| 82 | heap[j] = heap[c] | 82 | |
| 83 | j = c | 83 | |
| 84 | if size: | 84 | |
| 85 | heap[j] = x | 85 | |
| 86 | 86 | ||
| 87 | b = heap[0] | 87 | |
| 88 | size -= 1 | 88 | |
| 89 | x = heap[size] if size else 0 | 89 | |
| 90 | j = 0 | 90 | |
| 91 | while True: | 91 | |
| 92 | l = (j << 1) + 1 | 92 | |
| 93 | if l >= size: | 93 | |
| 94 | break | 94 | |
| 95 | r = l + 1 | 95 | |
| 96 | c = l | 96 | |
| 97 | if r < size and heap[r] < heap[l]: | 97 | |
| 98 | c = r | 98 | |
| 99 | if heap[c] >= x: | 99 | |
| 100 | break | 100 | |
| 101 | heap[j] = heap[c] | 101 | |
| 102 | j = c | 102 | |
| 103 | if size: | 103 | |
| 104 | heap[j] = x | 104 | |
| 105 | 105 | ||
| 106 | s = a + b | 106 | |
| 107 | total += s | 107 | |
| 108 | 108 | ||
| 109 | j = size | 109 | |
| 110 | size += 1 | 110 | |
| 111 | while j: | 111 | |
| 112 | p = (j - 1) >> 1 | 112 | |
| 113 | if heap[p] <= s: | 113 | |
| 114 | break | 114 | |
| 115 | heap[j] = heap[p] | 115 | |
| 116 | j = p | 116 | |
| 117 | if j < len(heap): | 117 | |
| 118 | heap[j] = s | 118 | |
| 119 | else: | 119 | |
| 120 | heap.append(s) | 120 | |
| 121 | 121 | ||
| 122 | return {"encoded_length": total, "decoded": text} | 122 |
1def solve(input):2 text = input.get("text", "")3 n = len(text)4 if n == 0:5 return {"encoded_length": 0, "decoded": ""}67 counts = [0] * 2568 ascii_only = True9 unique = 01011 for ch in text:12 o = ord(ch)13 if o < 256:14 if counts[o] == 0:15 unique += 116 counts[o] += 117 else:18 ascii_only = False19 break2021 if ascii_only:22 if unique == 1:23 return {"encoded_length": n, "decoded": text}2425 heap = [0] * unique26 idx = 027 for c in counts:28 if c:29 heap[idx] = c30 idx += 131 else:32 d = {}33 get = d.get34 for ch in text:35 d[ch] = get(ch, 0) + 136 if len(d) == 1:37 return {"encoded_length": n, "decoded": text}38 heap = [0] * len(d)39 idx = 040 for c in d.values():41 heap[idx] = c42 idx += 14344 size = len(heap)4546 i = (size >> 1) - 147 while i >= 0:48 j = i49 x = heap[j]50 while True:51 l = (j << 1) + 152 if l >= size:53 break54 r = l + 155 c = l56 if r < size and heap[r] < heap[l]:57 c = r58 if heap[c] >= x:59 break60 heap[j] = heap[c]61 j = c62 heap[j] = x63 i -= 16465 total = 06667 while size > 1:68 a = heap[0]69 size -= 170 x = heap[size]71 j = 072 while True:73 l = (j << 1) + 174 if l >= size:75 break76 r = l + 177 c = l78 if r < size and heap[r] < heap[l]:79 c = r80 if heap[c] >= x:81 break82 heap[j] = heap[c]83 j = c84 if size:85 heap[j] = x8687 b = heap[0]88 size -= 189 x = heap[size] if size else 090 j = 091 while True:92 l = (j << 1) + 193 if l >= size:94 break95 r = l + 196 c = l97 if r < size and heap[r] < heap[l]:98 c = r99 if heap[c] >= x:100 break101 heap[j] = heap[c]102 j = c103 if size:104 heap[j] = x105106 s = a + b107 total += s108109 j = size110 size += 1111 while j:112 p = (j - 1) >> 1113 if heap[p] <= s:114 break115 heap[j] = heap[p]116 j = p117 if j < len(heap):118 heap[j] = s119 else:120 heap.append(s)121122 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}