Solution #68ce1629-b992-44da-ac45-25798e38ce45

completed

Score

100% (5/5)

Runtime

42μs

Delta

No change vs parent

Tied for best

Same as parent

Solution Lineage

Current100%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 = {}
    for ch in text:
        counts[ch] = counts.get(ch, 0) + 1

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

    total = 0
    work = freqs[:]
    active = m
    while active > 1:
        a = work.pop(0)
        b = work.pop(0)
        s = a + b
        total += s

        lo, hi = 0, active - 2
        while lo < hi:
            mid = (lo + hi) >> 1
            if work[mid] < s:
                lo = mid + 1
            else:
                hi = mid
        if lo < active - 2 and work[lo] < s:
            lo += 1
        work.insert(lo, s)
        active -= 1

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

Compare with Champion

Score Difference

Tied

Runtime Advantage

18μs slower

Code Size

37 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 for ch in text:8 get = counts.get
9 counts[ch] = counts.get(ch, 0) + 19 for ch in text:
1010 counts[ch] = get(ch, 0) + 1
11 freqs = sorted(counts.values())11
12 m = len(freqs)12 freqs = list(counts.values())
13 if m == 1:13 m = len(freqs)
14 return {"encoded_length": n, "decoded": text}14
1515 if m == 1:
16 total = 016 return {"encoded_length": n, "decoded": text}
17 work = freqs[:]17
18 active = m18 freqs.sort()
19 while active > 1:19
20 a = work.pop(0)20 # Bottom-up two-queue Huffman merge:
21 b = work.pop(0)21 # use freqs as first queue and merged as second queue.
22 s = a + b22 merged = [0] * (m - 1)
23 total += s23 i = j = k = 0
2424 total = 0
25 lo, hi = 0, active - 225
26 while lo < hi:26 while k < m - 1:
27 mid = (lo + hi) >> 127 if i < m and (j >= k or freqs[i] <= merged[j]):
28 if work[mid] < s:28 a = freqs[i]
29 lo = mid + 129 i += 1
30 else:30 else:
31 hi = mid31 a = merged[j]
32 if lo < active - 2 and work[lo] < s:32 j += 1
33 lo += 133
34 work.insert(lo, s)34 if i < m and (j >= k or freqs[i] <= merged[j]):
35 active -= 135 b = freqs[i]
3636 i += 1
37 return {"encoded_length": total, "decoded": text}37 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)42μ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 for ch in text:
9 counts[ch] = counts.get(ch, 0) + 1
10
11 freqs = sorted(counts.values())
12 m = len(freqs)
13 if m == 1:
14 return {"encoded_length": n, "decoded": text}
15
16 total = 0
17 work = freqs[:]
18 active = m
19 while active > 1:
20 a = work.pop(0)
21 b = work.pop(0)
22 s = a + b
23 total += s
24
25 lo, hi = 0, active - 2
26 while lo < hi:
27 mid = (lo + hi) >> 1
28 if work[mid] < s:
29 lo = mid + 1
30 else:
31 hi = mid
32 if lo < active - 2 and work[lo] < s:
33 lo += 1
34 work.insert(lo, s)
35 active -= 1
36
37 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}