Solution #96012705-a644-4e7e-b0e0-3d2f937dd240
completedScore
100% (5/5)
Runtime
46μs
Delta
No change vs parent
Tied for best
Same as parent
Score
100% (5/5)
Runtime
46μ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": ""}
try:
b = text.encode("latin1")
counts = [0] * 256
for x in b:
counts[x] += 1
vals = [c for c in counts if c]
except:
cps = [ord(ch) for ch in text]
cps.sort()
vals = []
prev = cps[0]
cnt = 1
for x in cps[1:]:
if x == prev:
cnt += 1
else:
vals.append(cnt)
prev = x
cnt = 1
vals.append(cnt)
m = len(vals)
if m == 1:
return {"encoded_length": n, "decoded": text}
vals.sort()
q = [0] * (m - 1)
i = j = k = total = 0
while k < m - 1:
if i < m and (j >= k or vals[i] <= q[j]):
a = vals[i]
i += 1
else:
a = q[j]
j += 1
if i < m and (j >= k or vals[i] <= q[j]):
b = vals[i]
i += 1
else:
b = q[j]
j += 1
s = a + b
total += s
q[k] = s
k += 1
return {"encoded_length": total, "decoded": text}Score Difference
Tied
Runtime Advantage
22μs slower
Code Size
57 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 | try: | 7 | counts = {} |
| 8 | b = text.encode("latin1") | 8 | get = counts.get |
| 9 | counts = [0] * 256 | 9 | for ch in text: |
| 10 | for x in b: | 10 | counts[ch] = get(ch, 0) + 1 |
| 11 | counts[x] += 1 | 11 | |
| 12 | 12 | freqs = list(counts.values()) | |
| 13 | vals = [c for c in counts if c] | 13 | m = len(freqs) |
| 14 | except: | 14 | |
| 15 | cps = [ord(ch) for ch in text] | 15 | if m == 1: |
| 16 | cps.sort() | 16 | return {"encoded_length": n, "decoded": text} |
| 17 | vals = [] | 17 | |
| 18 | prev = cps[0] | 18 | freqs.sort() |
| 19 | cnt = 1 | 19 | |
| 20 | for x in cps[1:]: | 20 | # Bottom-up two-queue Huffman merge: |
| 21 | if x == prev: | 21 | # use freqs as first queue and merged as second queue. |
| 22 | cnt += 1 | 22 | merged = [0] * (m - 1) |
| 23 | else: | 23 | i = j = k = 0 |
| 24 | vals.append(cnt) | 24 | total = 0 |
| 25 | prev = x | 25 | |
| 26 | cnt = 1 | 26 | while k < m - 1: |
| 27 | vals.append(cnt) | 27 | if i < m and (j >= k or freqs[i] <= merged[j]): |
| 28 | 28 | a = freqs[i] | |
| 29 | m = len(vals) | 29 | i += 1 |
| 30 | if m == 1: | 30 | else: |
| 31 | return {"encoded_length": n, "decoded": text} | 31 | a = merged[j] |
| 32 | 32 | j += 1 | |
| 33 | vals.sort() | 33 | |
| 34 | q = [0] * (m - 1) | 34 | if i < m and (j >= k or freqs[i] <= merged[j]): |
| 35 | i = j = k = total = 0 | 35 | b = freqs[i] |
| 36 | 36 | i += 1 | |
| 37 | while k < m - 1: | 37 | else: |
| 38 | if i < m and (j >= k or vals[i] <= q[j]): | 38 | b = merged[j] |
| 39 | a = vals[i] | 39 | j += 1 |
| 40 | i += 1 | 40 | |
| 41 | else: | 41 | s = a + b |
| 42 | a = q[j] | 42 | merged[k] = s |
| 43 | j += 1 | 43 | total += s |
| 44 | 44 | k += 1 | |
| 45 | if i < m and (j >= k or vals[i] <= q[j]): | 45 | |
| 46 | b = vals[i] | 46 | return {"encoded_length": total, "decoded": text} |
| 47 | i += 1 | 47 | |
| 48 | else: | 48 | |
| 49 | b = q[j] | 49 | |
| 50 | j += 1 | 50 | |
| 51 | 51 | ||
| 52 | s = a + b | 52 | |
| 53 | total += s | 53 | |
| 54 | q[k] = s | 54 | |
| 55 | k += 1 | 55 | |
| 56 | 56 | ||
| 57 | return {"encoded_length": total, "decoded": text} | 57 |
1def solve(input):2 text = input.get("text", "")3 n = len(text)4 if n == 0:5 return {"encoded_length": 0, "decoded": ""}67 try:8 b = text.encode("latin1")9 counts = [0] * 25610 for x in b:11 counts[x] += 11213 vals = [c for c in counts if c]14 except:15 cps = [ord(ch) for ch in text]16 cps.sort()17 vals = []18 prev = cps[0]19 cnt = 120 for x in cps[1:]:21 if x == prev:22 cnt += 123 else:24 vals.append(cnt)25 prev = x26 cnt = 127 vals.append(cnt)2829 m = len(vals)30 if m == 1:31 return {"encoded_length": n, "decoded": text}3233 vals.sort()34 q = [0] * (m - 1)35 i = j = k = total = 03637 while k < m - 1:38 if i < m and (j >= k or vals[i] <= q[j]):39 a = vals[i]40 i += 141 else:42 a = q[j]43 j += 14445 if i < m and (j >= k or vals[i] <= q[j]):46 b = vals[i]47 i += 148 else:49 b = q[j]50 j += 15152 s = a + b53 total += s54 q[k] = s55 k += 15657 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}