Solution #4911088c-d5f3-458e-891b-4b2c402c62dc
completedScore
100% (5/5)
Runtime
210μs
Delta
No change vs parent
Tied for best
Same as parent
Score
100% (5/5)
Runtime
210μs
Delta
No change vs parent
Tied for best
Same as parent
def solve(input):
text = input.get("text", "")
n = len(text)
if not n:
return {"encoded_length": 0, "decoded": ""}
class Node:
__slots__ = ("w", "left", "right")
def __init__(self, w, left=-1, right=-1):
self.w = w
self.left = left
self.right = right
counts = {}
get = counts.get
for ch in text:
counts[ch] = get(ch, 0) + 1
m = len(counts)
if m == 1:
return {"encoded_length": n, "decoded": text}
nodes = []
append = nodes.append
for w in counts.values():
append(Node(w))
order = sorted(range(m), key=lambda i: nodes[i].w)
q1 = order
q2 = [0] * (m - 1)
h1 = 0
t1 = m
h2 = 0
t2 = 0
next_idx = m
def pop_min():
nonlocal h1, h2
if h1 < t1:
if h2 < t2 and nodes[q2[h2]].w < nodes[q1[h1]].w:
idx = q2[h2]
h2 += 1
return idx
idx = q1[h1]
h1 += 1
return idx
idx = q2[h2]
h2 += 1
return idx
for _ in range(m - 1):
a = pop_min()
b = pop_min()
w = nodes[a].w + nodes[b].w
append(Node(w, a, b))
q2[t2] = next_idx
t2 += 1
next_idx += 1
total = 0
stack = [(next_idx - 1, 0)]
pop = stack.pop
push = stack.append
while stack:
idx, d = pop()
node = nodes[idx]
left = node.left
if left == -1:
total += node.w * d
else:
nd = d + 1
push((left, nd))
push((node.right, nd))
return {"encoded_length": total, "decoded": text}Score Difference
Tied
Runtime Advantage
186μs slower
Code Size
76 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 not n: | 4 | if n == 0: |
| 5 | return {"encoded_length": 0, "decoded": ""} | 5 | return {"encoded_length": 0, "decoded": ""} |
| 6 | 6 | ||
| 7 | class Node: | 7 | counts = {} |
| 8 | __slots__ = ("w", "left", "right") | 8 | get = counts.get |
| 9 | def __init__(self, w, left=-1, right=-1): | 9 | for ch in text: |
| 10 | self.w = w | 10 | counts[ch] = get(ch, 0) + 1 |
| 11 | self.left = left | 11 | |
| 12 | self.right = right | 12 | freqs = list(counts.values()) |
| 13 | 13 | m = len(freqs) | |
| 14 | counts = {} | 14 | |
| 15 | get = counts.get | 15 | if m == 1: |
| 16 | for ch in text: | 16 | return {"encoded_length": n, "decoded": text} |
| 17 | counts[ch] = get(ch, 0) + 1 | 17 | |
| 18 | 18 | freqs.sort() | |
| 19 | m = len(counts) | 19 | |
| 20 | if m == 1: | 20 | # Bottom-up two-queue Huffman merge: |
| 21 | return {"encoded_length": n, "decoded": text} | 21 | # use freqs as first queue and merged as second queue. |
| 22 | 22 | merged = [0] * (m - 1) | |
| 23 | nodes = [] | 23 | i = j = k = 0 |
| 24 | append = nodes.append | 24 | total = 0 |
| 25 | for w in counts.values(): | 25 | |
| 26 | append(Node(w)) | 26 | while k < m - 1: |
| 27 | 27 | if i < m and (j >= k or freqs[i] <= merged[j]): | |
| 28 | order = sorted(range(m), key=lambda i: nodes[i].w) | 28 | a = freqs[i] |
| 29 | 29 | i += 1 | |
| 30 | q1 = order | 30 | else: |
| 31 | q2 = [0] * (m - 1) | 31 | a = merged[j] |
| 32 | h1 = 0 | 32 | j += 1 |
| 33 | t1 = m | 33 | |
| 34 | h2 = 0 | 34 | if i < m and (j >= k or freqs[i] <= merged[j]): |
| 35 | t2 = 0 | 35 | b = freqs[i] |
| 36 | next_idx = m | 36 | i += 1 |
| 37 | 37 | else: | |
| 38 | def pop_min(): | 38 | b = merged[j] |
| 39 | nonlocal h1, h2 | 39 | j += 1 |
| 40 | if h1 < t1: | 40 | |
| 41 | if h2 < t2 and nodes[q2[h2]].w < nodes[q1[h1]].w: | 41 | s = a + b |
| 42 | idx = q2[h2] | 42 | merged[k] = s |
| 43 | h2 += 1 | 43 | total += s |
| 44 | return idx | 44 | k += 1 |
| 45 | idx = q1[h1] | 45 | |
| 46 | h1 += 1 | 46 | return {"encoded_length": total, "decoded": text} |
| 47 | return idx | 47 | |
| 48 | idx = q2[h2] | 48 | |
| 49 | h2 += 1 | 49 | |
| 50 | return idx | 50 | |
| 51 | 51 | ||
| 52 | for _ in range(m - 1): | 52 | |
| 53 | a = pop_min() | 53 | |
| 54 | b = pop_min() | 54 | |
| 55 | w = nodes[a].w + nodes[b].w | 55 | |
| 56 | append(Node(w, a, b)) | 56 | |
| 57 | q2[t2] = next_idx | 57 | |
| 58 | t2 += 1 | 58 | |
| 59 | next_idx += 1 | 59 | |
| 60 | 60 | ||
| 61 | total = 0 | 61 | |
| 62 | stack = [(next_idx - 1, 0)] | 62 | |
| 63 | pop = stack.pop | 63 | |
| 64 | push = stack.append | 64 | |
| 65 | while stack: | 65 | |
| 66 | idx, d = pop() | 66 | |
| 67 | node = nodes[idx] | 67 | |
| 68 | left = node.left | 68 | |
| 69 | if left == -1: | 69 | |
| 70 | total += node.w * d | 70 | |
| 71 | else: | 71 | |
| 72 | nd = d + 1 | 72 | |
| 73 | push((left, nd)) | 73 | |
| 74 | push((node.right, nd)) | 74 | |
| 75 | 75 | ||
| 76 | return {"encoded_length": total, "decoded": text} | 76 |
1def solve(input):2 text = input.get("text", "")3 n = len(text)4 if not n:5 return {"encoded_length": 0, "decoded": ""}67 class Node:8 __slots__ = ("w", "left", "right")9 def __init__(self, w, left=-1, right=-1):10 self.w = w11 self.left = left12 self.right = right1314 counts = {}15 get = counts.get16 for ch in text:17 counts[ch] = get(ch, 0) + 11819 m = len(counts)20 if m == 1:21 return {"encoded_length": n, "decoded": text}2223 nodes = []24 append = nodes.append25 for w in counts.values():26 append(Node(w))2728 order = sorted(range(m), key=lambda i: nodes[i].w)2930 q1 = order31 q2 = [0] * (m - 1)32 h1 = 033 t1 = m34 h2 = 035 t2 = 036 next_idx = m3738 def pop_min():39 nonlocal h1, h240 if h1 < t1:41 if h2 < t2 and nodes[q2[h2]].w < nodes[q1[h1]].w:42 idx = q2[h2]43 h2 += 144 return idx45 idx = q1[h1]46 h1 += 147 return idx48 idx = q2[h2]49 h2 += 150 return idx5152 for _ in range(m - 1):53 a = pop_min()54 b = pop_min()55 w = nodes[a].w + nodes[b].w56 append(Node(w, a, b))57 q2[t2] = next_idx58 t2 += 159 next_idx += 16061 total = 062 stack = [(next_idx - 1, 0)]63 pop = stack.pop64 push = stack.append65 while stack:66 idx, d = pop()67 node = nodes[idx]68 left = node.left69 if left == -1:70 total += node.w * d71 else:72 nd = d + 173 push((left, nd))74 push((node.right, nd))7576 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}