Solution #6a54e553-004a-4797-8155-665343bbf36b
completedScore
100% (5/5)
Runtime
62μs
Delta
No change vs parent
Tied for best
Same as parent
Score
100% (5/5)
Runtime
62μ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 = {}
get = counts.get
for ch in text:
counts[ch] = get(ch, 0) + 1
if len(counts) == 1:
return {"encoded_length": n, "decoded": text}
heap = [0] * len(counts)
k = 0
for v in counts.values():
heap[k] = v
k += 1
for i in range((k >> 1) - 1, -1, -1):
j = i
x = heap[j]
while True:
l = (j << 1) + 1
if l >= k:
break
r = l + 1
c = l
if r < k and heap[r] < heap[l]:
c = r
if heap[c] >= x:
break
heap[j] = heap[c]
j = c
heap[j] = x
total = 0
size = k
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]
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
heap[j] = s
return {"encoded_length": total, "decoded": text}Score Difference
Tied
Runtime Advantage
38μs slower
Code Size
93 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 | get = counts.get | 8 | get = counts.get |
| 9 | for ch in text: | 9 | for ch in text: |
| 10 | counts[ch] = get(ch, 0) + 1 | 10 | counts[ch] = get(ch, 0) + 1 |
| 11 | 11 | ||
| 12 | if len(counts) == 1: | 12 | freqs = list(counts.values()) |
| 13 | return {"encoded_length": n, "decoded": text} | 13 | m = len(freqs) |
| 14 | 14 | ||
| 15 | heap = [0] * len(counts) | 15 | if m == 1: |
| 16 | k = 0 | 16 | return {"encoded_length": n, "decoded": text} |
| 17 | for v in counts.values(): | 17 | |
| 18 | heap[k] = v | 18 | freqs.sort() |
| 19 | k += 1 | 19 | |
| 20 | 20 | # Bottom-up two-queue Huffman merge: | |
| 21 | for i in range((k >> 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 >= k: | 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 = l | 29 | i += 1 |
| 30 | if r < k and heap[r] < heap[l]: | 30 | else: |
| 31 | c = r | 31 | a = merged[j] |
| 32 | if heap[c] >= x: | 32 | j += 1 |
| 33 | break | 33 | |
| 34 | heap[j] = heap[c] | 34 | if i < m and (j >= k or freqs[i] <= merged[j]): |
| 35 | j = c | 35 | b = freqs[i] |
| 36 | heap[j] = x | 36 | i += 1 |
| 37 | 37 | else: | |
| 38 | total = 0 | 38 | b = merged[j] |
| 39 | size = k | 39 | j += 1 |
| 40 | 40 | ||
| 41 | while size > 1: | 41 | s = a + b |
| 42 | a = heap[0] | 42 | merged[k] = s |
| 43 | size -= 1 | 43 | total += s |
| 44 | x = heap[size] | 44 | k += 1 |
| 45 | j = 0 | 45 | |
| 46 | while True: | 46 | return {"encoded_length": total, "decoded": text} |
| 47 | l = (j << 1) + 1 | 47 | |
| 48 | if l >= size: | 48 | |
| 49 | break | 49 | |
| 50 | r = l + 1 | 50 | |
| 51 | c = l | 51 | |
| 52 | if r < size and heap[r] < heap[l]: | 52 | |
| 53 | c = r | 53 | |
| 54 | if heap[c] >= x: | 54 | |
| 55 | break | 55 | |
| 56 | heap[j] = heap[c] | 56 | |
| 57 | j = c | 57 | |
| 58 | if size: | 58 | |
| 59 | heap[j] = x | 59 | |
| 60 | 60 | ||
| 61 | b = heap[0] | 61 | |
| 62 | size -= 1 | 62 | |
| 63 | x = heap[size] | 63 | |
| 64 | j = 0 | 64 | |
| 65 | while True: | 65 | |
| 66 | l = (j << 1) + 1 | 66 | |
| 67 | if l >= size: | 67 | |
| 68 | break | 68 | |
| 69 | r = l + 1 | 69 | |
| 70 | c = l | 70 | |
| 71 | if r < size and heap[r] < heap[l]: | 71 | |
| 72 | c = r | 72 | |
| 73 | if heap[c] >= x: | 73 | |
| 74 | break | 74 | |
| 75 | heap[j] = heap[c] | 75 | |
| 76 | j = c | 76 | |
| 77 | if size: | 77 | |
| 78 | heap[j] = x | 78 | |
| 79 | 79 | ||
| 80 | s = a + b | 80 | |
| 81 | total += s | 81 | |
| 82 | 82 | ||
| 83 | j = size | 83 | |
| 84 | size += 1 | 84 | |
| 85 | while j: | 85 | |
| 86 | p = (j - 1) >> 1 | 86 | |
| 87 | if heap[p] <= s: | 87 | |
| 88 | break | 88 | |
| 89 | heap[j] = heap[p] | 89 | |
| 90 | j = p | 90 | |
| 91 | heap[j] = s | 91 | |
| 92 | 92 | ||
| 93 | return {"encoded_length": total, "decoded": text} | 93 |
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 if len(counts) == 1:13 return {"encoded_length": n, "decoded": text}1415 heap = [0] * len(counts)16 k = 017 for v in counts.values():18 heap[k] = v19 k += 12021 for i in range((k >> 1) - 1, -1, -1):22 j = i23 x = heap[j]24 while True:25 l = (j << 1) + 126 if l >= k:27 break28 r = l + 129 c = l30 if r < k and heap[r] < heap[l]:31 c = r32 if heap[c] >= x:33 break34 heap[j] = heap[c]35 j = c36 heap[j] = x3738 total = 039 size = k4041 while size > 1:42 a = heap[0]43 size -= 144 x = heap[size]45 j = 046 while True:47 l = (j << 1) + 148 if l >= size:49 break50 r = l + 151 c = l52 if r < size and heap[r] < heap[l]:53 c = r54 if heap[c] >= x:55 break56 heap[j] = heap[c]57 j = c58 if size:59 heap[j] = x6061 b = heap[0]62 size -= 163 x = heap[size]64 j = 065 while True:66 l = (j << 1) + 167 if l >= size:68 break69 r = l + 170 c = l71 if r < size and heap[r] < heap[l]:72 c = r73 if heap[c] >= x:74 break75 heap[j] = heap[c]76 j = c77 if size:78 heap[j] = x7980 s = a + b81 total += s8283 j = size84 size += 185 while j:86 p = (j - 1) >> 187 if heap[p] <= s:88 break89 heap[j] = heap[p]90 j = p91 heap[j] = s9293 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}