Solution #917a89f1-b1b0-4947-9dde-152b287ea4f8
completedScore
20% (1/5)
Runtime
147μs
Delta
-80.0% vs parent
-80.0% vs best
Regression from parent
Score
20% (1/5)
Runtime
147μs
Delta
-80.0% 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 = {}
for ch in text:
counts[ch] = counts.get(ch, 0) + 1
if len(counts) == 1:
return {"encoded_length": n, "decoded": text}
# In-place binary min-heap over frequencies using a single growing list.
heap = []
push = heap.append
for v in counts.values():
push(v)
# Heapify
i = (len(heap) >> 1) - 1
while i >= 0:
j = i
x = heap[j]
size = len(heap)
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
y = heap[c]
if x <= y:
break
heap[j] = y
j = c
heap[j] = x
i -= 1
total = 0
while len(heap) > 1:
size = len(heap)
a = heap[0]
x = heap[size - 1]
if size == 2:
heap.pop()
else:
heap[0] = x
heap.pop()
size -= 1
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
y = heap[c]
if x <= y:
break
heap[j] = y
j = c
heap[j] = x
size = len(heap)
b = heap[0]
x = heap[size - 1]
if size == 2:
heap.pop()
else:
heap[0] = x
heap.pop()
size -= 1
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
y = heap[c]
if x <= y:
break
heap[j] = y
j = c
heap[j] = x
s = a + b
total += s
heap.append(s)
j = len(heap) - 1
while j:
p = (j - 1) >> 1
y = heap[p]
if y <= s:
break
heap[j] = y
j = p
heap[j] = s
return {"encoded_length": total, "decoded": text}Score Difference
-80.0%
Runtime Advantage
123μs slower
Code Size
110 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 | for ch in text: | 8 | get = counts.get |
| 9 | counts[ch] = counts.get(ch, 0) + 1 | 9 | for ch in text: |
| 10 | 10 | counts[ch] = get(ch, 0) + 1 | |
| 11 | if len(counts) == 1: | 11 | |
| 12 | return {"encoded_length": n, "decoded": text} | 12 | freqs = list(counts.values()) |
| 13 | 13 | m = len(freqs) | |
| 14 | # In-place binary min-heap over frequencies using a single growing list. | 14 | |
| 15 | heap = [] | 15 | if m == 1: |
| 16 | push = heap.append | 16 | return {"encoded_length": n, "decoded": text} |
| 17 | for v in counts.values(): | 17 | |
| 18 | push(v) | 18 | freqs.sort() |
| 19 | 19 | ||
| 20 | # Heapify | 20 | # Bottom-up two-queue Huffman merge: |
| 21 | i = (len(heap) >> 1) - 1 | 21 | # use freqs as first queue and merged as second queue. |
| 22 | while i >= 0: | 22 | merged = [0] * (m - 1) |
| 23 | j = i | 23 | i = j = k = 0 |
| 24 | x = heap[j] | 24 | total = 0 |
| 25 | size = len(heap) | 25 | |
| 26 | while True: | 26 | while k < m - 1: |
| 27 | l = (j << 1) + 1 | 27 | if i < m and (j >= k or freqs[i] <= merged[j]): |
| 28 | if l >= size: | 28 | a = freqs[i] |
| 29 | break | 29 | i += 1 |
| 30 | r = l + 1 | 30 | else: |
| 31 | c = l | 31 | a = merged[j] |
| 32 | if r < size and heap[r] < heap[l]: | 32 | j += 1 |
| 33 | c = r | 33 | |
| 34 | y = heap[c] | 34 | if i < m and (j >= k or freqs[i] <= merged[j]): |
| 35 | if x <= y: | 35 | b = freqs[i] |
| 36 | break | 36 | i += 1 |
| 37 | heap[j] = y | 37 | else: |
| 38 | j = c | 38 | b = merged[j] |
| 39 | heap[j] = x | 39 | j += 1 |
| 40 | i -= 1 | 40 | |
| 41 | 41 | s = a + b | |
| 42 | total = 0 | 42 | merged[k] = s |
| 43 | 43 | total += s | |
| 44 | while len(heap) > 1: | 44 | k += 1 |
| 45 | size = len(heap) | 45 | |
| 46 | 46 | return {"encoded_length": total, "decoded": text} | |
| 47 | a = heap[0] | 47 | |
| 48 | x = heap[size - 1] | 48 | |
| 49 | if size == 2: | 49 | |
| 50 | heap.pop() | 50 | |
| 51 | else: | 51 | |
| 52 | heap[0] = x | 52 | |
| 53 | heap.pop() | 53 | |
| 54 | size -= 1 | 54 | |
| 55 | j = 0 | 55 | |
| 56 | while True: | 56 | |
| 57 | l = (j << 1) + 1 | 57 | |
| 58 | if l >= size: | 58 | |
| 59 | break | 59 | |
| 60 | r = l + 1 | 60 | |
| 61 | c = l | 61 | |
| 62 | if r < size and heap[r] < heap[l]: | 62 | |
| 63 | c = r | 63 | |
| 64 | y = heap[c] | 64 | |
| 65 | if x <= y: | 65 | |
| 66 | break | 66 | |
| 67 | heap[j] = y | 67 | |
| 68 | j = c | 68 | |
| 69 | heap[j] = x | 69 | |
| 70 | 70 | ||
| 71 | size = len(heap) | 71 | |
| 72 | b = heap[0] | 72 | |
| 73 | x = heap[size - 1] | 73 | |
| 74 | if size == 2: | 74 | |
| 75 | heap.pop() | 75 | |
| 76 | else: | 76 | |
| 77 | heap[0] = x | 77 | |
| 78 | heap.pop() | 78 | |
| 79 | size -= 1 | 79 | |
| 80 | j = 0 | 80 | |
| 81 | while True: | 81 | |
| 82 | l = (j << 1) + 1 | 82 | |
| 83 | if l >= size: | 83 | |
| 84 | break | 84 | |
| 85 | r = l + 1 | 85 | |
| 86 | c = l | 86 | |
| 87 | if r < size and heap[r] < heap[l]: | 87 | |
| 88 | c = r | 88 | |
| 89 | y = heap[c] | 89 | |
| 90 | if x <= y: | 90 | |
| 91 | break | 91 | |
| 92 | heap[j] = y | 92 | |
| 93 | j = c | 93 | |
| 94 | heap[j] = x | 94 | |
| 95 | 95 | ||
| 96 | s = a + b | 96 | |
| 97 | total += s | 97 | |
| 98 | 98 | ||
| 99 | heap.append(s) | 99 | |
| 100 | j = len(heap) - 1 | 100 | |
| 101 | while j: | 101 | |
| 102 | p = (j - 1) >> 1 | 102 | |
| 103 | y = heap[p] | 103 | |
| 104 | if y <= s: | 104 | |
| 105 | break | 105 | |
| 106 | heap[j] = y | 106 | |
| 107 | j = p | 107 | |
| 108 | heap[j] = s | 108 | |
| 109 | 109 | ||
| 110 | return {"encoded_length": total, "decoded": text} | 110 |
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 for ch in text:9 counts[ch] = counts.get(ch, 0) + 11011 if len(counts) == 1:12 return {"encoded_length": n, "decoded": text}1314 # In-place binary min-heap over frequencies using a single growing list.15 heap = []16 push = heap.append17 for v in counts.values():18 push(v)1920 # Heapify21 i = (len(heap) >> 1) - 122 while i >= 0:23 j = i24 x = heap[j]25 size = len(heap)26 while True:27 l = (j << 1) + 128 if l >= size:29 break30 r = l + 131 c = l32 if r < size and heap[r] < heap[l]:33 c = r34 y = heap[c]35 if x <= y:36 break37 heap[j] = y38 j = c39 heap[j] = x40 i -= 14142 total = 04344 while len(heap) > 1:45 size = len(heap)4647 a = heap[0]48 x = heap[size - 1]49 if size == 2:50 heap.pop()51 else:52 heap[0] = x53 heap.pop()54 size -= 155 j = 056 while True:57 l = (j << 1) + 158 if l >= size:59 break60 r = l + 161 c = l62 if r < size and heap[r] < heap[l]:63 c = r64 y = heap[c]65 if x <= y:66 break67 heap[j] = y68 j = c69 heap[j] = x7071 size = len(heap)72 b = heap[0]73 x = heap[size - 1]74 if size == 2:75 heap.pop()76 else:77 heap[0] = x78 heap.pop()79 size -= 180 j = 081 while True:82 l = (j << 1) + 183 if l >= size:84 break85 r = l + 186 c = l87 if r < size and heap[r] < heap[l]:88 c = r89 y = heap[c]90 if x <= y:91 break92 heap[j] = y93 j = c94 heap[j] = x9596 s = a + b97 total += s9899 heap.append(s)100 j = len(heap) - 1101 while j:102 p = (j - 1) >> 1103 y = heap[p]104 if y <= s:105 break106 heap[j] = y107 j = p108 heap[j] = s109110 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}