Solution #9e0f203b-0b3b-4808-b201-3bd67715b71e

completed

Score

100% (5/5)

Runtime

82μs

Delta

No change vs parent

Tied for best

Same as parent

Solution Lineage

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

    heap = []
    push = heap.append
    for v in counts.values():
        push(v)

    m = len(heap)

    for i in range((m >> 1) - 1, -1, -1):
        j = i
        x = heap[j]
        while True:
            l = (j << 1) + 1
            if l >= m:
                break
            r = l + 1
            c = r if r < m and heap[r] < heap[l] else l
            if heap[c] >= x:
                break
            heap[j] = heap[c]
            j = c
        heap[j] = x

    def heappop():
        nonlocal m
        root = heap[0]
        m -= 1
        if m:
            x = heap[m]
            j = 0
            while True:
                l = (j << 1) + 1
                if l >= m:
                    break
                r = l + 1
                c = r if r < m and heap[r] < heap[l] else l
                if heap[c] >= x:
                    break
                heap[j] = heap[c]
                j = c
            heap[j] = x
        return root

    def heappush(x):
        nonlocal m
        if m < len(heap):
            heap[m] = x
        else:
            heap.append(x)
        j = m
        m += 1
        while j:
            p = (j - 1) >> 1
            if heap[p] <= x:
                break
            heap[j] = heap[p]
            j = p
        heap[j] = x

    total = 0
    while m > 1:
        s = heappop() + heappop()
        total += s
        heappush(s)

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

Compare with Champion

Score Difference

Tied

Runtime Advantage

58μs slower

Code Size

78 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 heap = []14
15 push = heap.append15 if m == 1:
16 for v in counts.values():16 return {"encoded_length": n, "decoded": text}
17 push(v)17
1818 freqs.sort()
19 m = len(heap)19
2020 # Bottom-up two-queue Huffman merge:
21 for i in range((m >> 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 >= m: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 = r if r < m and heap[r] < heap[l] else l29 i += 1
30 if heap[c] >= x:30 else:
31 break31 a = merged[j]
32 heap[j] = heap[c]32 j += 1
33 j = c33
34 heap[j] = x34 if i < m and (j >= k or freqs[i] <= merged[j]):
3535 b = freqs[i]
36 def heappop():36 i += 1
37 nonlocal m37 else:
38 root = heap[0]38 b = merged[j]
39 m -= 139 j += 1
40 if m:40
41 x = heap[m]41 s = a + b
42 j = 042 merged[k] = s
43 while True:43 total += s
44 l = (j << 1) + 144 k += 1
45 if l >= m:45
46 break46 return {"encoded_length": total, "decoded": text}
47 r = l + 147
48 c = r if r < m and heap[r] < heap[l] else l48
49 if heap[c] >= x:49
50 break50
51 heap[j] = heap[c]51
52 j = c52
53 heap[j] = x53
54 return root54
5555
56 def heappush(x):56
57 nonlocal m57
58 if m < len(heap):58
59 heap[m] = x59
60 else:60
61 heap.append(x)61
62 j = m62
63 m += 163
64 while j:64
65 p = (j - 1) >> 165
66 if heap[p] <= x:66
67 break67
68 heap[j] = heap[p]68
69 j = p69
70 heap[j] = x70
7171
72 total = 072
73 while m > 1:73
74 s = heappop() + heappop()74
75 total += s75
76 heappush(s)76
7777
78 return {"encoded_length": total, "decoded": text}78
Your Solution
100% (5/5)82μ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 heap = []
15 push = heap.append
16 for v in counts.values():
17 push(v)
18
19 m = len(heap)
20
21 for i in range((m >> 1) - 1, -1, -1):
22 j = i
23 x = heap[j]
24 while True:
25 l = (j << 1) + 1
26 if l >= m:
27 break
28 r = l + 1
29 c = r if r < m and heap[r] < heap[l] else l
30 if heap[c] >= x:
31 break
32 heap[j] = heap[c]
33 j = c
34 heap[j] = x
35
36 def heappop():
37 nonlocal m
38 root = heap[0]
39 m -= 1
40 if m:
41 x = heap[m]
42 j = 0
43 while True:
44 l = (j << 1) + 1
45 if l >= m:
46 break
47 r = l + 1
48 c = r if r < m and heap[r] < heap[l] else l
49 if heap[c] >= x:
50 break
51 heap[j] = heap[c]
52 j = c
53 heap[j] = x
54 return root
55
56 def heappush(x):
57 nonlocal m
58 if m < len(heap):
59 heap[m] = x
60 else:
61 heap.append(x)
62 j = m
63 m += 1
64 while j:
65 p = (j - 1) >> 1
66 if heap[p] <= x:
67 break
68 heap[j] = heap[p]
69 j = p
70 heap[j] = x
71
72 total = 0
73 while m > 1:
74 s = heappop() + heappop()
75 total += s
76 heappush(s)
77
78 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}