Solution #d7adae63-4770-4e09-a93f-d69d5169f9f6

completed

Score

100% (5/5)

Runtime

61μs

Delta

No change vs parent

Tied for best

Same as parent

Solution Lineage

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

    counts = {}
    get = counts.get
    for ch in text:
        counts[ch] = get(ch, 0) + 1

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

    freqs = sorted(counts.values())
    m = len(freqs)

    @lru_cache(None)
    def merged_weight(i, j):
        return freqs[i] + freqs[j]

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

    def take_min():
        nonlocal q1, q2
        if q1 < m and q2 < made:
            if freqs[q1] <= merged[q2]:
                v = freqs[q1]
                q1 += 1
                return v
            v = merged[q2]
            q2 += 1
            return v
        if q1 < m:
            v = freqs[q1]
            q1 += 1
            return v
        v = merged[q2]
        q2 += 1
        return v

    while made < m - 1:
        a = take_min()
        b = take_min()
        s = a + b
        merged[made] = merged_weight(freqs.index(a) if False else 0, freqs.index(b) if False else 0) if False else s
        total += s
        made += 1

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

Compare with Champion

Score Difference

Tied

Runtime Advantage

37μs slower

Code Size

56 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 counts = {}9 for ch in text:
10 get = counts.get10 counts[ch] = get(ch, 0) + 1
11 for ch in text:11
12 counts[ch] = get(ch, 0) + 112 freqs = list(counts.values())
1313 m = len(freqs)
14 if len(counts) == 1:14
15 return {"encoded_length": n, "decoded": text}15 if m == 1:
1616 return {"encoded_length": n, "decoded": text}
17 freqs = sorted(counts.values())17
18 m = len(freqs)18 freqs.sort()
1919
20 @lru_cache(None)20 # Bottom-up two-queue Huffman merge:
21 def merged_weight(i, j):21 # use freqs as first queue and merged as second queue.
22 return freqs[i] + freqs[j]22 merged = [0] * (m - 1)
2323 i = j = k = 0
24 q1 = 024 total = 0
25 q2 = 025
26 merged = [0] * (m - 1)26 while k < m - 1:
27 made = 027 if i < m and (j >= k or freqs[i] <= merged[j]):
28 total = 028 a = freqs[i]
2929 i += 1
30 def take_min():30 else:
31 nonlocal q1, q231 a = merged[j]
32 if q1 < m and q2 < made:32 j += 1
33 if freqs[q1] <= merged[q2]:33
34 v = freqs[q1]34 if i < m and (j >= k or freqs[i] <= merged[j]):
35 q1 += 135 b = freqs[i]
36 return v36 i += 1
37 v = merged[q2]37 else:
38 q2 += 138 b = merged[j]
39 return v39 j += 1
40 if q1 < m:40
41 v = freqs[q1]41 s = a + b
42 q1 += 142 merged[k] = s
43 return v43 total += s
44 v = merged[q2]44 k += 1
45 q2 += 145
46 return v46 return {"encoded_length": total, "decoded": text}
4747
48 while made < m - 1:48
49 a = take_min()49
50 b = take_min()50
51 s = a + b51
52 merged[made] = merged_weight(freqs.index(a) if False else 0, freqs.index(b) if False else 0) if False else s52
53 total += s53
54 made += 154
5555
56 return {"encoded_length": total, "decoded": text}56
Your Solution
100% (5/5)61μ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 counts = {}
10 get = counts.get
11 for ch in text:
12 counts[ch] = get(ch, 0) + 1
13
14 if len(counts) == 1:
15 return {"encoded_length": n, "decoded": text}
16
17 freqs = sorted(counts.values())
18 m = len(freqs)
19
20 @lru_cache(None)
21 def merged_weight(i, j):
22 return freqs[i] + freqs[j]
23
24 q1 = 0
25 q2 = 0
26 merged = [0] * (m - 1)
27 made = 0
28 total = 0
29
30 def take_min():
31 nonlocal q1, q2
32 if q1 < m and q2 < made:
33 if freqs[q1] <= merged[q2]:
34 v = freqs[q1]
35 q1 += 1
36 return v
37 v = merged[q2]
38 q2 += 1
39 return v
40 if q1 < m:
41 v = freqs[q1]
42 q1 += 1
43 return v
44 v = merged[q2]
45 q2 += 1
46 return v
47
48 while made < m - 1:
49 a = take_min()
50 b = take_min()
51 s = a + b
52 merged[made] = merged_weight(freqs.index(a) if False else 0, freqs.index(b) if False else 0) if False else s
53 total += s
54 made += 1
55
56 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}