Solution #96773990-608b-44da-a8ad-1e9df695bea7
completedScore
100% (5/5)
Runtime
92μs
Delta
No change vs parent
Tied for best
Same as parent
Score
100% (5/5)
Runtime
92μ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": ""}
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}Score Difference
Tied
Runtime Advantage
68μs slower
Code Size
74 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 | from functools import lru_cache | 7 | counts = {} |
| 8 | 8 | 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] * 256 | 11 | |
| 12 | all_ascii = True | 12 | 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] += 1 | 16 | return {"encoded_length": n, "decoded": text} |
| 17 | else: | 17 | |
| 18 | all_ascii = False | 18 | freqs.sort() |
| 19 | break | 19 | |
| 20 | return all_ascii, tuple(arr) | 20 | # Bottom-up two-queue Huffman merge: |
| 21 | 21 | # use freqs as first queue and merged as second queue. | |
| 22 | all_ascii, arr = count_ascii(text) | 22 | merged = [0] * (m - 1) |
| 23 | 23 | 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.get | 28 | a = freqs[i] |
| 29 | for ch in text: | 29 | i += 1 |
| 30 | counts[ch] = get(ch, 0) + 1 | 30 | else: |
| 31 | freqs = list(counts.values()) | 31 | a = merged[j] |
| 32 | 32 | 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] |
| 36 | 36 | i += 1 | |
| 37 | freqs.sort() | 37 | else: |
| 38 | 38 | 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 0 | 44 | k += 1 |
| 45 | 45 | ||
| 46 | q1 = 0 | 46 | return {"encoded_length": total, "decoded": text} |
| 47 | q2 = 0 | 47 | |
| 48 | made = 0 | 48 | |
| 49 | merged = [0] * (m - 1) | 49 | |
| 50 | total = 0 | 50 | |
| 51 | 51 | ||
| 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 += 1 | 55 | |
| 56 | else: | 56 | |
| 57 | a = merged[q2] | 57 | |
| 58 | q2 += 1 | 58 | |
| 59 | 59 | ||
| 60 | if q1 < m and (q2 >= made or vals[q1] <= merged[q2]): | 60 | |
| 61 | b = vals[q1] | 61 | |
| 62 | q1 += 1 | 62 | |
| 63 | else: | 63 | |
| 64 | b = merged[q2] | 64 | |
| 65 | q2 += 1 | 65 | |
| 66 | 66 | ||
| 67 | s = a + b | 67 | |
| 68 | merged[made] = s | 68 | |
| 69 | total += s | 69 | |
| 70 | made += 1 | 70 | |
| 71 | 71 | ||
| 72 | return total | 72 | |
| 73 | 73 | ||
| 74 | return {"encoded_length": merged_sum(tuple(freqs)), "decoded": text} | 74 |
1def solve(input):2 text = input.get("text", "")3 n = len(text)4 if n == 0:5 return {"encoded_length": 0, "decoded": ""}67 from functools import lru_cache89 @lru_cache(None)10 def count_ascii(s):11 arr = [0] * 25612 all_ascii = True13 for ch in s:14 o = ord(ch)15 if o < 256:16 arr[o] += 117 else:18 all_ascii = False19 break20 return all_ascii, tuple(arr)2122 all_ascii, arr = count_ascii(text)2324 if all_ascii:25 freqs = [c for c in arr if c]26 else:27 counts = {}28 get = counts.get29 for ch in text:30 counts[ch] = get(ch, 0) + 131 freqs = list(counts.values())3233 m = len(freqs)34 if m == 1:35 return {"encoded_length": n, "decoded": text}3637 freqs.sort()3839 @lru_cache(None)40 def merged_sum(sorted_freqs):41 vals = list(sorted_freqs)42 m = len(vals)43 if m <= 1:44 return 04546 q1 = 047 q2 = 048 made = 049 merged = [0] * (m - 1)50 total = 05152 while made < m - 1:53 if q1 < m and (q2 >= made or vals[q1] <= merged[q2]):54 a = vals[q1]55 q1 += 156 else:57 a = merged[q2]58 q2 += 15960 if q1 < m and (q2 >= made or vals[q1] <= merged[q2]):61 b = vals[q1]62 q1 += 163 else:64 b = merged[q2]65 q2 += 16667 s = a + b68 merged[made] = s69 total += s70 made += 17172 return total7374 return {"encoded_length": merged_sum(tuple(freqs)), "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}