Solution #8607bb8f-ff8b-4e83-a30c-14f61cfa9d6b
completedScore
100% (5/5)
Runtime
71μs
Delta
No change vs parent
Tied for best
Same as parent
Score
100% (5/5)
Runtime
71μ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:
counts = {}
for ch in text:
counts[ch] = counts.get(ch, 0) + 1
vals = list(counts.values())
m = len(vals)
if m == 1:
return {"encoded_length": n, "decoded": text}
vals.sort()
def bisect_right(a, x, lo, hi):
while lo < hi:
mid = (lo + hi) >> 1
if x < a[mid]:
hi = mid
else:
lo = mid + 1
return lo
total = 0
merged = []
while m > 1:
a = vals[0]
b = vals[1]
s = a + b
total += s
del vals[:2]
pos = bisect_right(vals, s, 0, len(vals))
vals[pos:pos] = [s]
m -= 1
return {"encoded_length": total, "decoded": text}Score Difference
Tied
Runtime Advantage
47μs slower
Code Size
47 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 | vals = [c for c in counts if c] | 12 | freqs = list(counts.values()) |
| 13 | except: | 13 | m = len(freqs) |
| 14 | counts = {} | 14 | |
| 15 | for ch in text: | 15 | if m == 1: |
| 16 | counts[ch] = counts.get(ch, 0) + 1 | 16 | return {"encoded_length": n, "decoded": text} |
| 17 | vals = list(counts.values()) | 17 | |
| 18 | 18 | freqs.sort() | |
| 19 | m = len(vals) | 19 | |
| 20 | if m == 1: | 20 | # Bottom-up two-queue Huffman merge: |
| 21 | return {"encoded_length": n, "decoded": text} | 21 | # use freqs as first queue and merged as second queue. |
| 22 | 22 | merged = [0] * (m - 1) | |
| 23 | vals.sort() | 23 | i = j = k = 0 |
| 24 | 24 | total = 0 | |
| 25 | def bisect_right(a, x, lo, hi): | 25 | |
| 26 | while lo < hi: | 26 | while k < m - 1: |
| 27 | mid = (lo + hi) >> 1 | 27 | if i < m and (j >= k or freqs[i] <= merged[j]): |
| 28 | if x < a[mid]: | 28 | a = freqs[i] |
| 29 | hi = mid | 29 | i += 1 |
| 30 | else: | 30 | else: |
| 31 | lo = mid + 1 | 31 | a = merged[j] |
| 32 | return lo | 32 | j += 1 |
| 33 | 33 | ||
| 34 | total = 0 | 34 | if i < m and (j >= k or freqs[i] <= merged[j]): |
| 35 | merged = [] | 35 | b = freqs[i] |
| 36 | 36 | i += 1 | |
| 37 | while m > 1: | 37 | else: |
| 38 | a = vals[0] | 38 | b = merged[j] |
| 39 | b = vals[1] | 39 | j += 1 |
| 40 | s = a + b | 40 | |
| 41 | total += s | 41 | s = a + b |
| 42 | del vals[:2] | 42 | merged[k] = s |
| 43 | pos = bisect_right(vals, s, 0, len(vals)) | 43 | total += s |
| 44 | vals[pos:pos] = [s] | 44 | k += 1 |
| 45 | m -= 1 | 45 | |
| 46 | 46 | return {"encoded_length": total, "decoded": text} | |
| 47 | return {"encoded_length": total, "decoded": text} | 47 |
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] += 112 vals = [c for c in counts if c]13 except:14 counts = {}15 for ch in text:16 counts[ch] = counts.get(ch, 0) + 117 vals = list(counts.values())1819 m = len(vals)20 if m == 1:21 return {"encoded_length": n, "decoded": text}2223 vals.sort()2425 def bisect_right(a, x, lo, hi):26 while lo < hi:27 mid = (lo + hi) >> 128 if x < a[mid]:29 hi = mid30 else:31 lo = mid + 132 return lo3334 total = 035 merged = []3637 while m > 1:38 a = vals[0]39 b = vals[1]40 s = a + b41 total += s42 del vals[:2]43 pos = bisect_right(vals, s, 0, len(vals))44 vals[pos:pos] = [s]45 m -= 14647 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}