Solution #4b16e427-07d8-4ccb-9d87-fd1e02f4d97b

completed

Score

60% (3/5)

Runtime

32μs

Delta

-40.0% vs parent

-40.0% vs best

Regression from parent

Solution Lineage

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

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

    total = 0
    # In-place frequency accumulation via sorted run-length encoding.
    # This avoids a dict and builds the multiset of weights directly.
    s = sorted(text)

    # Single-symbol case handled via run counting
    weights = []
    prev = s[0]
    cnt = 1
    for i in range(1, n):
        ch = s[i]
        if ch == prev:
            cnt += 1
        else:
            weights.append(cnt)
            prev = ch
            cnt = 1
    weights.append(cnt)

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

    # weights already sorted because equal chars are grouped in sorted(text)
    # Two-queue Huffman merge without heap/list reinsertion.
    merged = [0] * (m - 1)
    i = 0
    j = 0
    k = 0

    def take():
        nonlocal i, j
        if i < m and (j >= k or weights[i] <= merged[j]):
            v = weights[i]
            i += 1
            return v
        v = merged[j]
        j += 1
        return v

    while k < m - 1:
        a = take()
        b = take()
        s2 = a + b
        total += s2
        merged[k] = s2
        k += 1

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

Compare with Champion

Score Difference

-40.0%

Runtime Advantage

8μs slower

Code Size

58 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 if n == 1:7 counts = {}
8 return {"encoded_length": 1, "decoded": text}8 get = counts.get
99 for ch in text:
10 total = 010 counts[ch] = get(ch, 0) + 1
11 # In-place frequency accumulation via sorted run-length encoding.11
12 # This avoids a dict and builds the multiset of weights directly.12 freqs = list(counts.values())
13 s = sorted(text)13 m = len(freqs)
1414
15 # Single-symbol case handled via run counting15 if m == 1:
16 weights = []16 return {"encoded_length": n, "decoded": text}
17 prev = s[0]17
18 cnt = 118 freqs.sort()
19 for i in range(1, n):19
20 ch = s[i]20 # Bottom-up two-queue Huffman merge:
21 if ch == prev:21 # use freqs as first queue and merged as second queue.
22 cnt += 122 merged = [0] * (m - 1)
23 else:23 i = j = k = 0
24 weights.append(cnt)24 total = 0
25 prev = ch25
26 cnt = 126 while k < m - 1:
27 weights.append(cnt)27 if i < m and (j >= k or freqs[i] <= merged[j]):
2828 a = freqs[i]
29 m = len(weights)29 i += 1
30 if m == 1:30 else:
31 return {"encoded_length": n, "decoded": text}31 a = merged[j]
3232 j += 1
33 # weights already sorted because equal chars are grouped in sorted(text)33
34 # Two-queue Huffman merge without heap/list reinsertion.34 if i < m and (j >= k or freqs[i] <= merged[j]):
35 merged = [0] * (m - 1)35 b = freqs[i]
36 i = 036 i += 1
37 j = 037 else:
38 k = 038 b = merged[j]
3939 j += 1
40 def take():40
41 nonlocal i, j41 s = a + b
42 if i < m and (j >= k or weights[i] <= merged[j]):42 merged[k] = s
43 v = weights[i]43 total += s
44 i += 144 k += 1
45 return v45
46 v = merged[j]46 return {"encoded_length": total, "decoded": text}
47 j += 147
48 return v48
4949
50 while k < m - 1:50
51 a = take()51
52 b = take()52
53 s2 = a + b53
54 total += s254
55 merged[k] = s255
56 k += 156
5757
58 return {"encoded_length": total, "decoded": text}58
Your Solution
60% (3/5)32μ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 if n == 1:
8 return {"encoded_length": 1, "decoded": text}
9
10 total = 0
11 # In-place frequency accumulation via sorted run-length encoding.
12 # This avoids a dict and builds the multiset of weights directly.
13 s = sorted(text)
14
15 # Single-symbol case handled via run counting
16 weights = []
17 prev = s[0]
18 cnt = 1
19 for i in range(1, n):
20 ch = s[i]
21 if ch == prev:
22 cnt += 1
23 else:
24 weights.append(cnt)
25 prev = ch
26 cnt = 1
27 weights.append(cnt)
28
29 m = len(weights)
30 if m == 1:
31 return {"encoded_length": n, "decoded": text}
32
33 # weights already sorted because equal chars are grouped in sorted(text)
34 # Two-queue Huffman merge without heap/list reinsertion.
35 merged = [0] * (m - 1)
36 i = 0
37 j = 0
38 k = 0
39
40 def take():
41 nonlocal i, j
42 if i < m and (j >= k or weights[i] <= merged[j]):
43 v = weights[i]
44 i += 1
45 return v
46 v = merged[j]
47 j += 1
48 return v
49
50 while k < m - 1:
51 a = take()
52 b = take()
53 s2 = a + b
54 total += s2
55 merged[k] = s2
56 k += 1
57
58 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}