Solution #9e0f203b-0b3b-4808-b201-3bd67715b71e
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 = {}
for ch in text:
counts[ch] = counts.get(ch, 0) + 1
if len(counts) == 1:
return {"encoded_length": n, "decoded": text}
heap = []
push = heap.append
for v in counts.values():
push(v)
m = len(heap)
for i in range((m >> 1) - 1, -1, -1):
j = i
x = heap[j]
while True:
l = (j << 1) + 1
if l >= m:
break
r = l + 1
c = r if r < m and heap[r] < heap[l] else l
if heap[c] >= x:
break
heap[j] = heap[c]
j = c
heap[j] = x
def heappop():
nonlocal m
root = heap[0]
m -= 1
if m:
x = heap[m]
j = 0
while True:
l = (j << 1) + 1
if l >= m:
break
r = l + 1
c = r if r < m and heap[r] < heap[l] else l
if heap[c] >= x:
break
heap[j] = heap[c]
j = c
heap[j] = x
return root
def heappush(x):
nonlocal m
if m < len(heap):
heap[m] = x
else:
heap.append(x)
j = m
m += 1
while j:
p = (j - 1) >> 1
if heap[p] <= x:
break
heap[j] = heap[p]
j = p
heap[j] = x
total = 0
while m > 1:
s = heappop() + heappop()
total += s
heappush(s)
return {"encoded_length": total, "decoded": text}Score Difference
Tied
Runtime Advantage
58μs slower
Code Size
78 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 | heap = [] | 14 | |
| 15 | push = heap.append | 15 | if m == 1: |
| 16 | for v in counts.values(): | 16 | return {"encoded_length": n, "decoded": text} |
| 17 | push(v) | 17 | |
| 18 | 18 | freqs.sort() | |
| 19 | m = len(heap) | 19 | |
| 20 | 20 | # Bottom-up two-queue Huffman merge: | |
| 21 | for i in range((m >> 1) - 1, -1, -1): | 21 | # use freqs as first queue and merged as second queue. |
| 22 | j = i | 22 | merged = [0] * (m - 1) |
| 23 | x = heap[j] | 23 | i = j = k = 0 |
| 24 | while True: | 24 | total = 0 |
| 25 | l = (j << 1) + 1 | 25 | |
| 26 | if l >= m: | 26 | while k < m - 1: |
| 27 | break | 27 | if i < m and (j >= k or freqs[i] <= merged[j]): |
| 28 | r = l + 1 | 28 | a = freqs[i] |
| 29 | c = r if r < m and heap[r] < heap[l] else l | 29 | i += 1 |
| 30 | if heap[c] >= x: | 30 | else: |
| 31 | break | 31 | a = merged[j] |
| 32 | heap[j] = heap[c] | 32 | j += 1 |
| 33 | j = c | 33 | |
| 34 | heap[j] = x | 34 | if i < m and (j >= k or freqs[i] <= merged[j]): |
| 35 | 35 | b = freqs[i] | |
| 36 | def heappop(): | 36 | i += 1 |
| 37 | nonlocal m | 37 | else: |
| 38 | root = heap[0] | 38 | b = merged[j] |
| 39 | m -= 1 | 39 | j += 1 |
| 40 | if m: | 40 | |
| 41 | x = heap[m] | 41 | s = a + b |
| 42 | j = 0 | 42 | merged[k] = s |
| 43 | while True: | 43 | total += s |
| 44 | l = (j << 1) + 1 | 44 | k += 1 |
| 45 | if l >= m: | 45 | |
| 46 | break | 46 | return {"encoded_length": total, "decoded": text} |
| 47 | r = l + 1 | 47 | |
| 48 | c = r if r < m and heap[r] < heap[l] else l | 48 | |
| 49 | if heap[c] >= x: | 49 | |
| 50 | break | 50 | |
| 51 | heap[j] = heap[c] | 51 | |
| 52 | j = c | 52 | |
| 53 | heap[j] = x | 53 | |
| 54 | return root | 54 | |
| 55 | 55 | ||
| 56 | def heappush(x): | 56 | |
| 57 | nonlocal m | 57 | |
| 58 | if m < len(heap): | 58 | |
| 59 | heap[m] = x | 59 | |
| 60 | else: | 60 | |
| 61 | heap.append(x) | 61 | |
| 62 | j = m | 62 | |
| 63 | m += 1 | 63 | |
| 64 | while j: | 64 | |
| 65 | p = (j - 1) >> 1 | 65 | |
| 66 | if heap[p] <= x: | 66 | |
| 67 | break | 67 | |
| 68 | heap[j] = heap[p] | 68 | |
| 69 | j = p | 69 | |
| 70 | heap[j] = x | 70 | |
| 71 | 71 | ||
| 72 | total = 0 | 72 | |
| 73 | while m > 1: | 73 | |
| 74 | s = heappop() + heappop() | 74 | |
| 75 | total += s | 75 | |
| 76 | heappush(s) | 76 | |
| 77 | 77 | ||
| 78 | return {"encoded_length": total, "decoded": text} | 78 |
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 heap = []15 push = heap.append16 for v in counts.values():17 push(v)1819 m = len(heap)2021 for i in range((m >> 1) - 1, -1, -1):22 j = i23 x = heap[j]24 while True:25 l = (j << 1) + 126 if l >= m:27 break28 r = l + 129 c = r if r < m and heap[r] < heap[l] else l30 if heap[c] >= x:31 break32 heap[j] = heap[c]33 j = c34 heap[j] = x3536 def heappop():37 nonlocal m38 root = heap[0]39 m -= 140 if m:41 x = heap[m]42 j = 043 while True:44 l = (j << 1) + 145 if l >= m:46 break47 r = l + 148 c = r if r < m and heap[r] < heap[l] else l49 if heap[c] >= x:50 break51 heap[j] = heap[c]52 j = c53 heap[j] = x54 return root5556 def heappush(x):57 nonlocal m58 if m < len(heap):59 heap[m] = x60 else:61 heap.append(x)62 j = m63 m += 164 while j:65 p = (j - 1) >> 166 if heap[p] <= x:67 break68 heap[j] = heap[p]69 j = p70 heap[j] = x7172 total = 073 while m > 1:74 s = heappop() + heappop()75 total += s76 heappush(s)7778 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}