Solution #4911088c-d5f3-458e-891b-4b2c402c62dc

completed

Score

100% (5/5)

Runtime

210μs

Delta

No change vs parent

Tied for best

Same as parent

Solution Lineage

Current100%Same as parent
68ce1629100%Same as parent
2d8382ce100%Same as parent
62c233ed100%Same as parent
82a9952b100%Same as parent
b513de2d100%Same as parent
8607bb8f100%Same as parent
96012705100%Same as parent
21d15e87100%Same as parent
9a277008100%Improved from parent
1b2fa39920%Regression from parent
98fd02a3100%Same as parent
ae4d0f99100%Same as parent
6126deee100%Same as parent
d720f0bf100%Same as parent
7e637902100%Same as parent
29bbc470100%Same as parent
268b5b53100%Same as parent
ffe6e932100%Same as parent
bb8a4da9100%Same as parent
0d32fca6100%Same as parent
195f1ac7100%Same as parent
96773990100%Same as parent
d7adae63100%Same as parent
8cb031c4100%Same as parent
0826d84e100%Same as parent
2da814bd100%Same as parent
e227904f100%Same as parent
69696638100%Same as parent
c128503d100%Same as parent
9e0f203b100%Same as parent
30447971100%Same as parent
62082b2d100%Same as parent
2a708353100%Same as parent
a0f415b0100%First in chain

Code

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}

Compare with Champion

Score Difference

Tied

Runtime Advantage

186μs slower

Code Size

76 vs 46 lines

#Your Solution#Champion
1def solve(input):1def 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": ""}
66
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 = w10 counts[ch] = get(ch, 0) + 1
11 self.left = left11
12 self.right = right12 freqs = list(counts.values())
1313 m = len(freqs)
14 counts = {}14
15 get = counts.get15 if m == 1:
16 for ch in text:16 return {"encoded_length": n, "decoded": text}
17 counts[ch] = get(ch, 0) + 117
1818 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.
2222 merged = [0] * (m - 1)
23 nodes = []23 i = j = k = 0
24 append = nodes.append24 total = 0
25 for w in counts.values():25
26 append(Node(w))26 while k < m - 1:
2727 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]
2929 i += 1
30 q1 = order30 else:
31 q2 = [0] * (m - 1)31 a = merged[j]
32 h1 = 032 j += 1
33 t1 = m33
34 h2 = 034 if i < m and (j >= k or freqs[i] <= merged[j]):
35 t2 = 035 b = freqs[i]
36 next_idx = m36 i += 1
3737 else:
38 def pop_min():38 b = merged[j]
39 nonlocal h1, h239 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 += 143 total += s
44 return idx44 k += 1
45 idx = q1[h1]45
46 h1 += 146 return {"encoded_length": total, "decoded": text}
47 return idx47
48 idx = q2[h2]48
49 h2 += 149
50 return idx50
5151
52 for _ in range(m - 1):52
53 a = pop_min()53
54 b = pop_min()54
55 w = nodes[a].w + nodes[b].w55
56 append(Node(w, a, b))56
57 q2[t2] = next_idx57
58 t2 += 158
59 next_idx += 159
6060
61 total = 061
62 stack = [(next_idx - 1, 0)]62
63 pop = stack.pop63
64 push = stack.append64
65 while stack:65
66 idx, d = pop()66
67 node = nodes[idx]67
68 left = node.left68
69 if left == -1:69
70 total += node.w * d70
71 else:71
72 nd = d + 172
73 push((left, nd))73
74 push((node.right, nd))74
7575
76 return {"encoded_length": total, "decoded": text}76
Your Solution
100% (5/5)210μs
1def solve(input):
2 text = input.get("text", "")
3 n = len(text)
4 if not n:
5 return {"encoded_length": 0, "decoded": ""}
6
7 class Node:
8 __slots__ = ("w", "left", "right")
9 def __init__(self, w, left=-1, right=-1):
10 self.w = w
11 self.left = left
12 self.right = right
13
14 counts = {}
15 get = counts.get
16 for ch in text:
17 counts[ch] = get(ch, 0) + 1
18
19 m = len(counts)
20 if m == 1:
21 return {"encoded_length": n, "decoded": text}
22
23 nodes = []
24 append = nodes.append
25 for w in counts.values():
26 append(Node(w))
27
28 order = sorted(range(m), key=lambda i: nodes[i].w)
29
30 q1 = order
31 q2 = [0] * (m - 1)
32 h1 = 0
33 t1 = m
34 h2 = 0
35 t2 = 0
36 next_idx = m
37
38 def pop_min():
39 nonlocal h1, h2
40 if h1 < t1:
41 if h2 < t2 and nodes[q2[h2]].w < nodes[q1[h1]].w:
42 idx = q2[h2]
43 h2 += 1
44 return idx
45 idx = q1[h1]
46 h1 += 1
47 return idx
48 idx = q2[h2]
49 h2 += 1
50 return idx
51
52 for _ in range(m - 1):
53 a = pop_min()
54 b = pop_min()
55 w = nodes[a].w + nodes[b].w
56 append(Node(w, a, b))
57 q2[t2] = next_idx
58 t2 += 1
59 next_idx += 1
60
61 total = 0
62 stack = [(next_idx - 1, 0)]
63 pop = stack.pop
64 push = stack.append
65 while stack:
66 idx, d = pop()
67 node = nodes[idx]
68 left = node.left
69 if left == -1:
70 total += node.w * d
71 else:
72 nd = d + 1
73 push((left, nd))
74 push((node.right, nd))
75
76 return {"encoded_length": total, "decoded": text}
Champion
100% (5/5)24μs
1def solve(input):
2 text = input.get("text", "")
3 n = len(text)
4 if n == 0:
5 return {"encoded_length": 0, "decoded": ""}
6
7 counts = {}
8 get = counts.get
9 for ch in text:
10 counts[ch] = get(ch, 0) + 1
11
12 freqs = list(counts.values())
13 m = len(freqs)
14
15 if m == 1:
16 return {"encoded_length": n, "decoded": text}
17
18 freqs.sort()
19
20 # 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 = 0
24 total = 0
25
26 while k < m - 1:
27 if i < m and (j >= k or freqs[i] <= merged[j]):
28 a = freqs[i]
29 i += 1
30 else:
31 a = merged[j]
32 j += 1
33
34 if i < m and (j >= k or freqs[i] <= merged[j]):
35 b = freqs[i]
36 i += 1
37 else:
38 b = merged[j]
39 j += 1
40
41 s = a + b
42 merged[k] = s
43 total += s
44 k += 1
45
46 return {"encoded_length": total, "decoded": text}