Solution #96773990-608b-44da-a8ad-1e9df695bea7

completed

Score

100% (5/5)

Runtime

92μs

Delta

No change vs parent

Tied for best

Same as parent

Solution Lineage

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

    from functools import lru_cache

    @lru_cache(None)
    def count_ascii(s):
        arr = [0] * 256
        all_ascii = True
        for ch in s:
            o = ord(ch)
            if o < 256:
                arr[o] += 1
            else:
                all_ascii = False
                break
        return all_ascii, tuple(arr)

    all_ascii, arr = count_ascii(text)

    if all_ascii:
        freqs = [c for c in arr if c]
    else:
        counts = {}
        get = counts.get
        for ch in text:
            counts[ch] = get(ch, 0) + 1
        freqs = list(counts.values())

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

    freqs.sort()

    @lru_cache(None)
    def merged_sum(sorted_freqs):
        vals = list(sorted_freqs)
        m = len(vals)
        if m <= 1:
            return 0

        q1 = 0
        q2 = 0
        made = 0
        merged = [0] * (m - 1)
        total = 0

        while made < m - 1:
            if q1 < m and (q2 >= made or vals[q1] <= merged[q2]):
                a = vals[q1]
                q1 += 1
            else:
                a = merged[q2]
                q2 += 1

            if q1 < m and (q2 >= made or vals[q1] <= merged[q2]):
                b = vals[q1]
                q1 += 1
            else:
                b = merged[q2]
                q2 += 1

            s = a + b
            merged[made] = s
            total += s
            made += 1

        return total

    return {"encoded_length": merged_sum(tuple(freqs)), "decoded": text}

Compare with Champion

Score Difference

Tied

Runtime Advantage

68μs slower

Code Size

74 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 from functools import lru_cache7 counts = {}
88 get = counts.get
9 @lru_cache(None)9 for ch in text:
10 def count_ascii(s):10 counts[ch] = get(ch, 0) + 1
11 arr = [0] * 25611
12 all_ascii = True12 freqs = list(counts.values())
13 for ch in s:13 m = len(freqs)
14 o = ord(ch)14
15 if o < 256:15 if m == 1:
16 arr[o] += 116 return {"encoded_length": n, "decoded": text}
17 else:17
18 all_ascii = False18 freqs.sort()
19 break19
20 return all_ascii, tuple(arr)20 # Bottom-up two-queue Huffman merge:
2121 # use freqs as first queue and merged as second queue.
22 all_ascii, arr = count_ascii(text)22 merged = [0] * (m - 1)
2323 i = j = k = 0
24 if all_ascii:24 total = 0
25 freqs = [c for c in arr if c]25
26 else:26 while k < m - 1:
27 counts = {}27 if i < m and (j >= k or freqs[i] <= merged[j]):
28 get = counts.get28 a = freqs[i]
29 for ch in text:29 i += 1
30 counts[ch] = get(ch, 0) + 130 else:
31 freqs = list(counts.values())31 a = merged[j]
3232 j += 1
33 m = len(freqs)33
34 if m == 1:34 if i < m and (j >= k or freqs[i] <= merged[j]):
35 return {"encoded_length": n, "decoded": text}35 b = freqs[i]
3636 i += 1
37 freqs.sort()37 else:
3838 b = merged[j]
39 @lru_cache(None)39 j += 1
40 def merged_sum(sorted_freqs):40
41 vals = list(sorted_freqs)41 s = a + b
42 m = len(vals)42 merged[k] = s
43 if m <= 1:43 total += s
44 return 044 k += 1
4545
46 q1 = 046 return {"encoded_length": total, "decoded": text}
47 q2 = 047
48 made = 048
49 merged = [0] * (m - 1)49
50 total = 050
5151
52 while made < m - 1:52
53 if q1 < m and (q2 >= made or vals[q1] <= merged[q2]):53
54 a = vals[q1]54
55 q1 += 155
56 else:56
57 a = merged[q2]57
58 q2 += 158
5959
60 if q1 < m and (q2 >= made or vals[q1] <= merged[q2]):60
61 b = vals[q1]61
62 q1 += 162
63 else:63
64 b = merged[q2]64
65 q2 += 165
6666
67 s = a + b67
68 merged[made] = s68
69 total += s69
70 made += 170
7171
72 return total72
7373
74 return {"encoded_length": merged_sum(tuple(freqs)), "decoded": text}74
Your Solution
100% (5/5)92μ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 from functools import lru_cache
8
9 @lru_cache(None)
10 def count_ascii(s):
11 arr = [0] * 256
12 all_ascii = True
13 for ch in s:
14 o = ord(ch)
15 if o < 256:
16 arr[o] += 1
17 else:
18 all_ascii = False
19 break
20 return all_ascii, tuple(arr)
21
22 all_ascii, arr = count_ascii(text)
23
24 if all_ascii:
25 freqs = [c for c in arr if c]
26 else:
27 counts = {}
28 get = counts.get
29 for ch in text:
30 counts[ch] = get(ch, 0) + 1
31 freqs = list(counts.values())
32
33 m = len(freqs)
34 if m == 1:
35 return {"encoded_length": n, "decoded": text}
36
37 freqs.sort()
38
39 @lru_cache(None)
40 def merged_sum(sorted_freqs):
41 vals = list(sorted_freqs)
42 m = len(vals)
43 if m <= 1:
44 return 0
45
46 q1 = 0
47 q2 = 0
48 made = 0
49 merged = [0] * (m - 1)
50 total = 0
51
52 while made < m - 1:
53 if q1 < m and (q2 >= made or vals[q1] <= merged[q2]):
54 a = vals[q1]
55 q1 += 1
56 else:
57 a = merged[q2]
58 q2 += 1
59
60 if q1 < m and (q2 >= made or vals[q1] <= merged[q2]):
61 b = vals[q1]
62 q1 += 1
63 else:
64 b = merged[q2]
65 q2 += 1
66
67 s = a + b
68 merged[made] = s
69 total += s
70 made += 1
71
72 return total
73
74 return {"encoded_length": merged_sum(tuple(freqs)), "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}