Solution #917a89f1-b1b0-4947-9dde-152b287ea4f8

completed

Score

20% (1/5)

Runtime

147μs

Delta

-80.0% vs parent

-80.0% vs best

Regression from parent

Solution Lineage

Current20%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 = {}
    for ch in text:
        counts[ch] = counts.get(ch, 0) + 1

    if len(counts) == 1:
        return {"encoded_length": n, "decoded": text}

    # In-place binary min-heap over frequencies using a single growing list.
    heap = []
    push = heap.append
    for v in counts.values():
        push(v)

    # Heapify
    i = (len(heap) >> 1) - 1
    while i >= 0:
        j = i
        x = heap[j]
        size = len(heap)
        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
            y = heap[c]
            if x <= y:
                break
            heap[j] = y
            j = c
        heap[j] = x
        i -= 1

    total = 0

    while len(heap) > 1:
        size = len(heap)

        a = heap[0]
        x = heap[size - 1]
        if size == 2:
            heap.pop()
        else:
            heap[0] = x
            heap.pop()
            size -= 1
            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
                y = heap[c]
                if x <= y:
                    break
                heap[j] = y
                j = c
            heap[j] = x

        size = len(heap)
        b = heap[0]
        x = heap[size - 1]
        if size == 2:
            heap.pop()
        else:
            heap[0] = x
            heap.pop()
            size -= 1
            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
                y = heap[c]
                if x <= y:
                    break
                heap[j] = y
                j = c
            heap[j] = x

        s = a + b
        total += s

        heap.append(s)
        j = len(heap) - 1
        while j:
            p = (j - 1) >> 1
            y = heap[p]
            if y <= s:
                break
            heap[j] = y
            j = p
        heap[j] = s

    return {"encoded_length": total, "decoded": text}

Compare with Champion

Score Difference

-80.0%

Runtime Advantage

123μs slower

Code Size

110 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 for ch in text:8 get = counts.get
9 counts[ch] = counts.get(ch, 0) + 19 for ch in text:
1010 counts[ch] = get(ch, 0) + 1
11 if len(counts) == 1:11
12 return {"encoded_length": n, "decoded": text}12 freqs = list(counts.values())
1313 m = len(freqs)
14 # In-place binary min-heap over frequencies using a single growing list.14
15 heap = []15 if m == 1:
16 push = heap.append16 return {"encoded_length": n, "decoded": text}
17 for v in counts.values():17
18 push(v)18 freqs.sort()
1919
20 # Heapify20 # Bottom-up two-queue Huffman merge:
21 i = (len(heap) >> 1) - 121 # use freqs as first queue and merged as second queue.
22 while i >= 0:22 merged = [0] * (m - 1)
23 j = i23 i = j = k = 0
24 x = heap[j]24 total = 0
25 size = len(heap)25
26 while True:26 while k < m - 1:
27 l = (j << 1) + 127 if i < m and (j >= k or freqs[i] <= merged[j]):
28 if l >= size:28 a = freqs[i]
29 break29 i += 1
30 r = l + 130 else:
31 c = l31 a = merged[j]
32 if r < size and heap[r] < heap[l]:32 j += 1
33 c = r33
34 y = heap[c]34 if i < m and (j >= k or freqs[i] <= merged[j]):
35 if x <= y:35 b = freqs[i]
36 break36 i += 1
37 heap[j] = y37 else:
38 j = c38 b = merged[j]
39 heap[j] = x39 j += 1
40 i -= 140
4141 s = a + b
42 total = 042 merged[k] = s
4343 total += s
44 while len(heap) > 1:44 k += 1
45 size = len(heap)45
4646 return {"encoded_length": total, "decoded": text}
47 a = heap[0]47
48 x = heap[size - 1]48
49 if size == 2:49
50 heap.pop()50
51 else:51
52 heap[0] = x52
53 heap.pop()53
54 size -= 154
55 j = 055
56 while True:56
57 l = (j << 1) + 157
58 if l >= size:58
59 break59
60 r = l + 160
61 c = l61
62 if r < size and heap[r] < heap[l]:62
63 c = r63
64 y = heap[c]64
65 if x <= y:65
66 break66
67 heap[j] = y67
68 j = c68
69 heap[j] = x69
7070
71 size = len(heap)71
72 b = heap[0]72
73 x = heap[size - 1]73
74 if size == 2:74
75 heap.pop()75
76 else:76
77 heap[0] = x77
78 heap.pop()78
79 size -= 179
80 j = 080
81 while True:81
82 l = (j << 1) + 182
83 if l >= size:83
84 break84
85 r = l + 185
86 c = l86
87 if r < size and heap[r] < heap[l]:87
88 c = r88
89 y = heap[c]89
90 if x <= y:90
91 break91
92 heap[j] = y92
93 j = c93
94 heap[j] = x94
9595
96 s = a + b96
97 total += s97
9898
99 heap.append(s)99
100 j = len(heap) - 1100
101 while j:101
102 p = (j - 1) >> 1102
103 y = heap[p]103
104 if y <= s:104
105 break105
106 heap[j] = y106
107 j = p107
108 heap[j] = s108
109109
110 return {"encoded_length": total, "decoded": text}110
Your Solution
20% (1/5)147μ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 for ch in text:
9 counts[ch] = counts.get(ch, 0) + 1
10
11 if len(counts) == 1:
12 return {"encoded_length": n, "decoded": text}
13
14 # In-place binary min-heap over frequencies using a single growing list.
15 heap = []
16 push = heap.append
17 for v in counts.values():
18 push(v)
19
20 # Heapify
21 i = (len(heap) >> 1) - 1
22 while i >= 0:
23 j = i
24 x = heap[j]
25 size = len(heap)
26 while True:
27 l = (j << 1) + 1
28 if l >= size:
29 break
30 r = l + 1
31 c = l
32 if r < size and heap[r] < heap[l]:
33 c = r
34 y = heap[c]
35 if x <= y:
36 break
37 heap[j] = y
38 j = c
39 heap[j] = x
40 i -= 1
41
42 total = 0
43
44 while len(heap) > 1:
45 size = len(heap)
46
47 a = heap[0]
48 x = heap[size - 1]
49 if size == 2:
50 heap.pop()
51 else:
52 heap[0] = x
53 heap.pop()
54 size -= 1
55 j = 0
56 while True:
57 l = (j << 1) + 1
58 if l >= size:
59 break
60 r = l + 1
61 c = l
62 if r < size and heap[r] < heap[l]:
63 c = r
64 y = heap[c]
65 if x <= y:
66 break
67 heap[j] = y
68 j = c
69 heap[j] = x
70
71 size = len(heap)
72 b = heap[0]
73 x = heap[size - 1]
74 if size == 2:
75 heap.pop()
76 else:
77 heap[0] = x
78 heap.pop()
79 size -= 1
80 j = 0
81 while True:
82 l = (j << 1) + 1
83 if l >= size:
84 break
85 r = l + 1
86 c = l
87 if r < size and heap[r] < heap[l]:
88 c = r
89 y = heap[c]
90 if x <= y:
91 break
92 heap[j] = y
93 j = c
94 heap[j] = x
95
96 s = a + b
97 total += s
98
99 heap.append(s)
100 j = len(heap) - 1
101 while j:
102 p = (j - 1) >> 1
103 y = heap[p]
104 if y <= s:
105 break
106 heap[j] = y
107 j = p
108 heap[j] = s
109
110 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}