Solution #52d74aac-4748-47c9-ba8b-8f14a1a3c301

completed

Score

100% (5/5)

Runtime

123μs

Delta

+400.0% vs parent

Tied for best

Improved from parent

Solution Lineage

Current100%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": ""}

    def build_counts(i, counts):
        if i == n:
            return counts
        ch = text[i]
        counts[ch] = counts.get(ch, 0) + 1
        return build_counts(i + 1, counts)

    counts = build_counts(0, {})
    freqs = list(counts.values())
    m = len(freqs)

    if m == 1:
        return {"encoded_length": n, "decoded": text}

    def heapify_down(heap, size, i):
        l = (i << 1) + 1
        if l >= size:
            return
        r = l + 1
        c = r if r < size and heap[r] < heap[l] else l
        if heap[c] < heap[i]:
            heap[i], heap[c] = heap[c], heap[i]
            heapify_down(heap, size, c)

    def heapify_build(heap, i):
        if i < 0:
            return
        heapify_down(heap, len(heap), i)
        heapify_build(heap, i - 1)

    heap = freqs[:]
    heapify_build(heap, (len(heap) >> 1) - 1)

    def sift_up(heap, i):
        if i == 0:
            return
        p = (i - 1) >> 1
        if heap[i] < heap[p]:
            heap[i], heap[p] = heap[p], heap[i]
            sift_up(heap, p)

    def heappush(heap, x):
        heap.append(x)
        sift_up(heap, len(heap) - 1)

    def heappop(heap):
        root = heap[0]
        last = heap.pop()
        if heap:
            heap[0] = last
            heapify_down(heap, len(heap), 0)
        return root

    def merge_all(heap, total):
        if len(heap) == 1:
            return total
        a = heappop(heap)
        b = heappop(heap)
        s = a + b
        heappush(heap, s)
        return merge_all(heap, total + s)

    total = merge_all(heap, 0)
    return {"encoded_length": total, "decoded": text}

Compare with Champion

Score Difference

Tied

Runtime Advantage

99μs slower

Code Size

70 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 def build_counts(i, counts):7 counts = {}
8 if i == n:8 get = counts.get
9 return counts9 for ch in text:
10 ch = text[i]10 counts[ch] = get(ch, 0) + 1
11 counts[ch] = counts.get(ch, 0) + 111
12 return build_counts(i + 1, counts)12 freqs = list(counts.values())
1313 m = len(freqs)
14 counts = build_counts(0, {})14
15 freqs = list(counts.values())15 if m == 1:
16 m = len(freqs)16 return {"encoded_length": n, "decoded": text}
1717
18 if m == 1:18 freqs.sort()
19 return {"encoded_length": n, "decoded": text}19
2020 # Bottom-up two-queue Huffman merge:
21 def heapify_down(heap, size, i):21 # use freqs as first queue and merged as second queue.
22 l = (i << 1) + 122 merged = [0] * (m - 1)
23 if l >= size:23 i = j = k = 0
24 return24 total = 0
25 r = l + 125
26 c = r if r < size and heap[r] < heap[l] else l26 while k < m - 1:
27 if heap[c] < heap[i]:27 if i < m and (j >= k or freqs[i] <= merged[j]):
28 heap[i], heap[c] = heap[c], heap[i]28 a = freqs[i]
29 heapify_down(heap, size, c)29 i += 1
3030 else:
31 def heapify_build(heap, i):31 a = merged[j]
32 if i < 0:32 j += 1
33 return33
34 heapify_down(heap, len(heap), i)34 if i < m and (j >= k or freqs[i] <= merged[j]):
35 heapify_build(heap, i - 1)35 b = freqs[i]
3636 i += 1
37 heap = freqs[:]37 else:
38 heapify_build(heap, (len(heap) >> 1) - 1)38 b = merged[j]
3939 j += 1
40 def sift_up(heap, i):40
41 if i == 0:41 s = a + b
42 return42 merged[k] = s
43 p = (i - 1) >> 143 total += s
44 if heap[i] < heap[p]:44 k += 1
45 heap[i], heap[p] = heap[p], heap[i]45
46 sift_up(heap, p)46 return {"encoded_length": total, "decoded": text}
4747
48 def heappush(heap, x):48
49 heap.append(x)49
50 sift_up(heap, len(heap) - 1)50
5151
52 def heappop(heap):52
53 root = heap[0]53
54 last = heap.pop()54
55 if heap:55
56 heap[0] = last56
57 heapify_down(heap, len(heap), 0)57
58 return root58
5959
60 def merge_all(heap, total):60
61 if len(heap) == 1:61
62 return total62
63 a = heappop(heap)63
64 b = heappop(heap)64
65 s = a + b65
66 heappush(heap, s)66
67 return merge_all(heap, total + s)67
6868
69 total = merge_all(heap, 0)69
70 return {"encoded_length": total, "decoded": text}70
Your Solution
100% (5/5)123μ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 def build_counts(i, counts):
8 if i == n:
9 return counts
10 ch = text[i]
11 counts[ch] = counts.get(ch, 0) + 1
12 return build_counts(i + 1, counts)
13
14 counts = build_counts(0, {})
15 freqs = list(counts.values())
16 m = len(freqs)
17
18 if m == 1:
19 return {"encoded_length": n, "decoded": text}
20
21 def heapify_down(heap, size, i):
22 l = (i << 1) + 1
23 if l >= size:
24 return
25 r = l + 1
26 c = r if r < size and heap[r] < heap[l] else l
27 if heap[c] < heap[i]:
28 heap[i], heap[c] = heap[c], heap[i]
29 heapify_down(heap, size, c)
30
31 def heapify_build(heap, i):
32 if i < 0:
33 return
34 heapify_down(heap, len(heap), i)
35 heapify_build(heap, i - 1)
36
37 heap = freqs[:]
38 heapify_build(heap, (len(heap) >> 1) - 1)
39
40 def sift_up(heap, i):
41 if i == 0:
42 return
43 p = (i - 1) >> 1
44 if heap[i] < heap[p]:
45 heap[i], heap[p] = heap[p], heap[i]
46 sift_up(heap, p)
47
48 def heappush(heap, x):
49 heap.append(x)
50 sift_up(heap, len(heap) - 1)
51
52 def heappop(heap):
53 root = heap[0]
54 last = heap.pop()
55 if heap:
56 heap[0] = last
57 heapify_down(heap, len(heap), 0)
58 return root
59
60 def merge_all(heap, total):
61 if len(heap) == 1:
62 return total
63 a = heappop(heap)
64 b = heappop(heap)
65 s = a + b
66 heappush(heap, s)
67 return merge_all(heap, total + s)
68
69 total = merge_all(heap, 0)
70 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}