Solution #98fd02a3-0eba-4cc6-83a7-a935da063d8c

completed

Score

100% (5/5)

Runtime

110μs

Delta

No change vs parent

Tied for best

Same as parent

Solution Lineage

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

    from functools import lru_cache
    import heapq

    @lru_cache(maxsize=None)
    def frequency_signature(s):
        counts = {}
        for ch in s:
            counts[ch] = counts.get(ch, 0) + 1
        return tuple(sorted(counts.values()))

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

    @lru_cache(maxsize=None)
    def optimal_merge_cost(freqs_state):
        heap = list(freqs_state)
        heapq.heapify(heap)
        total = 0
        pop = heapq.heappop
        push = heapq.heappush
        while len(heap) > 1:
            s = pop(heap) + pop(heap)
            total += s
            push(heap, s)
        return total

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

Compare with Champion

Score Difference

Tied

Runtime Advantage

86μs slower

Code Size

34 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 = {}
8 import heapq8 get = counts.get
99 for ch in text:
10 @lru_cache(maxsize=None)10 counts[ch] = get(ch, 0) + 1
11 def frequency_signature(s):11
12 counts = {}12 freqs = list(counts.values())
13 for ch in s:13 m = len(freqs)
14 counts[ch] = counts.get(ch, 0) + 114
15 return tuple(sorted(counts.values()))15 if m == 1:
1616 return {"encoded_length": n, "decoded": text}
17 freqs = frequency_signature(text)17
18 if len(freqs) == 1:18 freqs.sort()
19 return {"encoded_length": n, "decoded": text}19
2020 # Bottom-up two-queue Huffman merge:
21 @lru_cache(maxsize=None)21 # use freqs as first queue and merged as second queue.
22 def optimal_merge_cost(freqs_state):22 merged = [0] * (m - 1)
23 heap = list(freqs_state)23 i = j = k = 0
24 heapq.heapify(heap)24 total = 0
25 total = 025
26 pop = heapq.heappop26 while k < m - 1:
27 push = heapq.heappush27 if i < m and (j >= k or freqs[i] <= merged[j]):
28 while len(heap) > 1:28 a = freqs[i]
29 s = pop(heap) + pop(heap)29 i += 1
30 total += s30 else:
31 push(heap, s)31 a = merged[j]
32 return total32 j += 1
3333
34 return {"encoded_length": optimal_merge_cost(freqs), "decoded": text}34 if i < m and (j >= k or freqs[i] <= merged[j]):
3535 b = freqs[i]
3636 i += 1
3737 else:
3838 b = merged[j]
3939 j += 1
4040
4141 s = a + b
4242 merged[k] = s
4343 total += s
4444 k += 1
4545
4646 return {"encoded_length": total, "decoded": text}
Your Solution
100% (5/5)110μ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 import heapq
9
10 @lru_cache(maxsize=None)
11 def frequency_signature(s):
12 counts = {}
13 for ch in s:
14 counts[ch] = counts.get(ch, 0) + 1
15 return tuple(sorted(counts.values()))
16
17 freqs = frequency_signature(text)
18 if len(freqs) == 1:
19 return {"encoded_length": n, "decoded": text}
20
21 @lru_cache(maxsize=None)
22 def optimal_merge_cost(freqs_state):
23 heap = list(freqs_state)
24 heapq.heapify(heap)
25 total = 0
26 pop = heapq.heappop
27 push = heapq.heappush
28 while len(heap) > 1:
29 s = pop(heap) + pop(heap)
30 total += s
31 push(heap, s)
32 return total
33
34 return {"encoded_length": optimal_merge_cost(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}