Solution #0826d84e-9385-48cb-80d5-c5e8fffc4e74
completedScore
100% (5/5)
Runtime
45μs
Delta
No change vs parent
Tied for best
Same as parent
Score
100% (5/5)
Runtime
45μs
Delta
No change vs parent
Tied for best
Same as parent
def solve(input):
text = input.get("text", "")
n = len(text)
if n == 0:
return {"encoded_length": 0, "decoded": ""}
counts = [0] * 256
non_ascii = None
for ch in text:
o = ord(ch)
if o < 256:
counts[o] += 1
else:
if non_ascii is None:
non_ascii = {}
non_ascii[ch] = non_ascii.get(ch, 0) + 1
freqs = [c for c in counts if c]
if non_ascii:
freqs.extend(non_ascii.values())
m = len(freqs)
if m == 1:
return {"encoded_length": n, "decoded": text}
freqs.sort()
i = 0
j = 0
merged = [0] * (m - 1)
k = 0
total = 0
if m >= 2:
a = freqs[0]
b = freqs[1]
s = a + b
total = s
merged[0] = s
i = 2
k = 1
while k < m - 1:
if i < m and (j >= k or freqs[i] <= merged[j]):
x = freqs[i]
i += 1
else:
x = merged[j]
j += 1
if i < m and (j >= k or freqs[i] <= merged[j]):
y = freqs[i]
i += 1
else:
y = merged[j]
j += 1
s = x + y
merged[k] = s
total += s
k += 1
return {"encoded_length": total, "decoded": text}Score Difference
Tied
Runtime Advantage
21μs slower
Code Size
64 vs 46 lines
| # | Your Solution | # | Champion |
|---|---|---|---|
| 1 | def solve(input): | 1 | def 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": ""} |
| 6 | 6 | ||
| 7 | counts = [0] * 256 | 7 | counts = {} |
| 8 | non_ascii = None | 8 | get = counts.get |
| 9 | 9 | for ch in text: | |
| 10 | for ch in text: | 10 | counts[ch] = get(ch, 0) + 1 |
| 11 | o = ord(ch) | 11 | |
| 12 | if o < 256: | 12 | freqs = list(counts.values()) |
| 13 | counts[o] += 1 | 13 | m = len(freqs) |
| 14 | else: | 14 | |
| 15 | if non_ascii is None: | 15 | if m == 1: |
| 16 | non_ascii = {} | 16 | return {"encoded_length": n, "decoded": text} |
| 17 | non_ascii[ch] = non_ascii.get(ch, 0) + 1 | 17 | |
| 18 | 18 | freqs.sort() | |
| 19 | freqs = [c for c in counts if c] | 19 | |
| 20 | if non_ascii: | 20 | # Bottom-up two-queue Huffman merge: |
| 21 | freqs.extend(non_ascii.values()) | 21 | # use freqs as first queue and merged as second queue. |
| 22 | 22 | merged = [0] * (m - 1) | |
| 23 | m = len(freqs) | 23 | i = j = k = 0 |
| 24 | if m == 1: | 24 | total = 0 |
| 25 | return {"encoded_length": n, "decoded": text} | 25 | |
| 26 | 26 | while k < m - 1: | |
| 27 | freqs.sort() | 27 | if i < m and (j >= k or freqs[i] <= merged[j]): |
| 28 | 28 | a = freqs[i] | |
| 29 | i = 0 | 29 | i += 1 |
| 30 | j = 0 | 30 | else: |
| 31 | merged = [0] * (m - 1) | 31 | a = merged[j] |
| 32 | k = 0 | 32 | j += 1 |
| 33 | total = 0 | 33 | |
| 34 | 34 | if i < m and (j >= k or freqs[i] <= merged[j]): | |
| 35 | if m >= 2: | 35 | b = freqs[i] |
| 36 | a = freqs[0] | 36 | i += 1 |
| 37 | b = freqs[1] | 37 | else: |
| 38 | s = a + b | 38 | b = merged[j] |
| 39 | total = s | 39 | j += 1 |
| 40 | merged[0] = s | 40 | |
| 41 | i = 2 | 41 | s = a + b |
| 42 | k = 1 | 42 | merged[k] = s |
| 43 | 43 | total += s | |
| 44 | while k < m - 1: | 44 | k += 1 |
| 45 | if i < m and (j >= k or freqs[i] <= merged[j]): | 45 | |
| 46 | x = freqs[i] | 46 | return {"encoded_length": total, "decoded": text} |
| 47 | i += 1 | 47 | |
| 48 | else: | 48 | |
| 49 | x = merged[j] | 49 | |
| 50 | j += 1 | 50 | |
| 51 | 51 | ||
| 52 | if i < m and (j >= k or freqs[i] <= merged[j]): | 52 | |
| 53 | y = freqs[i] | 53 | |
| 54 | i += 1 | 54 | |
| 55 | else: | 55 | |
| 56 | y = merged[j] | 56 | |
| 57 | j += 1 | 57 | |
| 58 | 58 | ||
| 59 | s = x + y | 59 | |
| 60 | merged[k] = s | 60 | |
| 61 | total += s | 61 | |
| 62 | k += 1 | 62 | |
| 63 | 63 | ||
| 64 | return {"encoded_length": total, "decoded": text} | 64 |
1def solve(input):2 text = input.get("text", "")3 n = len(text)4 if n == 0:5 return {"encoded_length": 0, "decoded": ""}67 counts = [0] * 2568 non_ascii = None910 for ch in text:11 o = ord(ch)12 if o < 256:13 counts[o] += 114 else:15 if non_ascii is None:16 non_ascii = {}17 non_ascii[ch] = non_ascii.get(ch, 0) + 11819 freqs = [c for c in counts if c]20 if non_ascii:21 freqs.extend(non_ascii.values())2223 m = len(freqs)24 if m == 1:25 return {"encoded_length": n, "decoded": text}2627 freqs.sort()2829 i = 030 j = 031 merged = [0] * (m - 1)32 k = 033 total = 03435 if m >= 2:36 a = freqs[0]37 b = freqs[1]38 s = a + b39 total = s40 merged[0] = s41 i = 242 k = 14344 while k < m - 1:45 if i < m and (j >= k or freqs[i] <= merged[j]):46 x = freqs[i]47 i += 148 else:49 x = merged[j]50 j += 15152 if i < m and (j >= k or freqs[i] <= merged[j]):53 y = freqs[i]54 i += 155 else:56 y = merged[j]57 j += 15859 s = x + y60 merged[k] = s61 total += s62 k += 16364 return {"encoded_length": total, "decoded": text}1def solve(input):2 text = input.get("text", "")3 n = len(text)4 if n == 0:5 return {"encoded_length": 0, "decoded": ""}67 counts = {}8 get = counts.get9 for ch in text:10 counts[ch] = get(ch, 0) + 11112 freqs = list(counts.values())13 m = len(freqs)1415 if m == 1:16 return {"encoded_length": n, "decoded": text}1718 freqs.sort()1920 # 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 = 024 total = 02526 while k < m - 1:27 if i < m and (j >= k or freqs[i] <= merged[j]):28 a = freqs[i]29 i += 130 else:31 a = merged[j]32 j += 13334 if i < m and (j >= k or freqs[i] <= merged[j]):35 b = freqs[i]36 i += 137 else:38 b = merged[j]39 j += 14041 s = a + b42 merged[k] = s43 total += s44 k += 14546 return {"encoded_length": total, "decoded": text}