Solution #06ab1c25-28af-4434-8eff-fb20d9e0838f

completed

Score

100% (5/5)

Runtime

115μs

Delta

No change vs parent

Tied for best

Same as parent

Solution Lineage

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

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

    s = ",".join(map(str, vals))
    nums = sorted(map(int, s.split(",")))

    i = 0
    j = 0
    q = ""
    qlen = 0
    total = 0
    m = len(nums)

    def take():
        nonlocal i, j, q, qlen
        if i < m:
            if j < qlen:
                k = q.find(",", j)
                qv = int(q[j:k])
                if nums[i] <= qv:
                    v = nums[i]
                    i += 1
                    return v
                j = k + 1
                return qv
            v = nums[i]
            i += 1
            return v
        k = q.find(",", j)
        v = int(q[j:k])
        j = k + 1
        return v

    while (m - i) + (qlen - j) // 2 > 1:
        a = take()
        b = take()
        s2 = a + b
        total += s2
        q += str(s2) + ","
        qlen = len(q)

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

Compare with Champion

Score Difference

Tied

Runtime Advantage

91μs slower

Code Size

54 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 vals = tuple(counts.values())12 freqs = list(counts.values())
13 if len(vals) == 1:13 m = len(freqs)
14 return {"encoded_length": n, "decoded": text}14
1515 if m == 1:
16 s = ",".join(map(str, vals))16 return {"encoded_length": n, "decoded": text}
17 nums = sorted(map(int, s.split(",")))17
1818 freqs.sort()
19 i = 019
20 j = 020 # Bottom-up two-queue Huffman merge:
21 q = ""21 # use freqs as first queue and merged as second queue.
22 qlen = 022 merged = [0] * (m - 1)
23 total = 023 i = j = k = 0
24 m = len(nums)24 total = 0
2525
26 def take():26 while k < m - 1:
27 nonlocal i, j, q, qlen27 if i < m and (j >= k or freqs[i] <= merged[j]):
28 if i < m:28 a = freqs[i]
29 if j < qlen:29 i += 1
30 k = q.find(",", j)30 else:
31 qv = int(q[j:k])31 a = merged[j]
32 if nums[i] <= qv:32 j += 1
33 v = nums[i]33
34 i += 134 if i < m and (j >= k or freqs[i] <= merged[j]):
35 return v35 b = freqs[i]
36 j = k + 136 i += 1
37 return qv37 else:
38 v = nums[i]38 b = merged[j]
39 i += 139 j += 1
40 return v40
41 k = q.find(",", j)41 s = a + b
42 v = int(q[j:k])42 merged[k] = s
43 j = k + 143 total += s
44 return v44 k += 1
4545
46 while (m - i) + (qlen - j) // 2 > 1:46 return {"encoded_length": total, "decoded": text}
47 a = take()47
48 b = take()48
49 s2 = a + b49
50 total += s250
51 q += str(s2) + ","51
52 qlen = len(q)52
5353
54 return {"encoded_length": total, "decoded": text}54
Your Solution
100% (5/5)115μ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 vals = tuple(counts.values())
13 if len(vals) == 1:
14 return {"encoded_length": n, "decoded": text}
15
16 s = ",".join(map(str, vals))
17 nums = sorted(map(int, s.split(",")))
18
19 i = 0
20 j = 0
21 q = ""
22 qlen = 0
23 total = 0
24 m = len(nums)
25
26 def take():
27 nonlocal i, j, q, qlen
28 if i < m:
29 if j < qlen:
30 k = q.find(",", j)
31 qv = int(q[j:k])
32 if nums[i] <= qv:
33 v = nums[i]
34 i += 1
35 return v
36 j = k + 1
37 return qv
38 v = nums[i]
39 i += 1
40 return v
41 k = q.find(",", j)
42 v = int(q[j:k])
43 j = k + 1
44 return v
45
46 while (m - i) + (qlen - j) // 2 > 1:
47 a = take()
48 b = take()
49 s2 = a + b
50 total += s2
51 q += str(s2) + ","
52 qlen = len(q)
53
54 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}