Solution #6a54e553-004a-4797-8155-665343bbf36b

completed

Score

100% (5/5)

Runtime

62μs

Delta

No change vs parent

Tied for best

Same as parent

Solution Lineage

Current100%Same as parent
8f72c514100%Improved from parent
9bcd8c5220%Regression from parent
1a8aec0060%Regression from parent
e2a7405b100%Same as parent
9e71593e100%Improved from parent
42ace40a40%Regression from parent
b404991f100%Same as parent
06ab1c25100%Same as parent
52d74aac100%Improved from parent
917a89f120%Regression from parent
f3599802100%Same as parent
6aeac392100%Improved from parent
3edec6f70%Regression from parent
f68f5746100%Same as parent
40369b8a100%Same as parent
f9d7018b100%Same as parent
5a4f6922100%Same as parent
15ec7bd1100%Same as parent
76aeaf5e100%Improved from parent
4b16e42760%Regression from parent
fd0fb3a5100%Same as parent
4911088c100%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 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}

Compare with Champion

Score Difference

Tied

Runtime Advantage

38μs slower

Code Size

93 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 n == 0:4 if n == 0:
5 return {"encoded_length": 0, "decoded": ""}5 return {"encoded_length": 0, "decoded": ""}
66
7 counts = {}7 counts = {}
8 get = counts.get8 get = counts.get
9 for ch in text:9 for ch in text:
10 counts[ch] = get(ch, 0) + 110 counts[ch] = get(ch, 0) + 1
1111
12 if len(counts) == 1:12 freqs = list(counts.values())
13 return {"encoded_length": n, "decoded": text}13 m = len(freqs)
1414
15 heap = [0] * len(counts)15 if m == 1:
16 k = 016 return {"encoded_length": n, "decoded": text}
17 for v in counts.values():17
18 heap[k] = v18 freqs.sort()
19 k += 119
2020 # 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 = i22 merged = [0] * (m - 1)
23 x = heap[j]23 i = j = k = 0
24 while True:24 total = 0
25 l = (j << 1) + 125
26 if l >= k:26 while k < m - 1:
27 break27 if i < m and (j >= k or freqs[i] <= merged[j]):
28 r = l + 128 a = freqs[i]
29 c = l29 i += 1
30 if r < k and heap[r] < heap[l]:30 else:
31 c = r31 a = merged[j]
32 if heap[c] >= x:32 j += 1
33 break33
34 heap[j] = heap[c]34 if i < m and (j >= k or freqs[i] <= merged[j]):
35 j = c35 b = freqs[i]
36 heap[j] = x36 i += 1
3737 else:
38 total = 038 b = merged[j]
39 size = k39 j += 1
4040
41 while size > 1:41 s = a + b
42 a = heap[0]42 merged[k] = s
43 size -= 143 total += s
44 x = heap[size]44 k += 1
45 j = 045
46 while True:46 return {"encoded_length": total, "decoded": text}
47 l = (j << 1) + 147
48 if l >= size:48
49 break49
50 r = l + 150
51 c = l51
52 if r < size and heap[r] < heap[l]:52
53 c = r53
54 if heap[c] >= x:54
55 break55
56 heap[j] = heap[c]56
57 j = c57
58 if size:58
59 heap[j] = x59
6060
61 b = heap[0]61
62 size -= 162
63 x = heap[size]63
64 j = 064
65 while True:65
66 l = (j << 1) + 166
67 if l >= size:67
68 break68
69 r = l + 169
70 c = l70
71 if r < size and heap[r] < heap[l]:71
72 c = r72
73 if heap[c] >= x:73
74 break74
75 heap[j] = heap[c]75
76 j = c76
77 if size:77
78 heap[j] = x78
7979
80 s = a + b80
81 total += s81
8282
83 j = size83
84 size += 184
85 while j:85
86 p = (j - 1) >> 186
87 if heap[p] <= s:87
88 break88
89 heap[j] = heap[p]89
90 j = p90
91 heap[j] = s91
9292
93 return {"encoded_length": total, "decoded": text}93
Your Solution
100% (5/5)62μ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 if len(counts) == 1:
13 return {"encoded_length": n, "decoded": text}
14
15 heap = [0] * len(counts)
16 k = 0
17 for v in counts.values():
18 heap[k] = v
19 k += 1
20
21 for i in range((k >> 1) - 1, -1, -1):
22 j = i
23 x = heap[j]
24 while True:
25 l = (j << 1) + 1
26 if l >= k:
27 break
28 r = l + 1
29 c = l
30 if r < k and heap[r] < heap[l]:
31 c = r
32 if heap[c] >= x:
33 break
34 heap[j] = heap[c]
35 j = c
36 heap[j] = x
37
38 total = 0
39 size = k
40
41 while size > 1:
42 a = heap[0]
43 size -= 1
44 x = heap[size]
45 j = 0
46 while True:
47 l = (j << 1) + 1
48 if l >= size:
49 break
50 r = l + 1
51 c = l
52 if r < size and heap[r] < heap[l]:
53 c = r
54 if heap[c] >= x:
55 break
56 heap[j] = heap[c]
57 j = c
58 if size:
59 heap[j] = x
60
61 b = heap[0]
62 size -= 1
63 x = heap[size]
64 j = 0
65 while True:
66 l = (j << 1) + 1
67 if l >= size:
68 break
69 r = l + 1
70 c = l
71 if r < size and heap[r] < heap[l]:
72 c = r
73 if heap[c] >= x:
74 break
75 heap[j] = heap[c]
76 j = c
77 if size:
78 heap[j] = x
79
80 s = a + b
81 total += s
82
83 j = size
84 size += 1
85 while j:
86 p = (j - 1) >> 1
87 if heap[p] <= s:
88 break
89 heap[j] = heap[p]
90 j = p
91 heap[j] = s
92
93 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}