Solution #5a4f6922-a0e9-4208-8265-cc218390e25b
completedScore
100% (5/5)
Runtime
84μs
Delta
No change vs parent
Tied for best
Same as parent
Score
100% (5/5)
Runtime
84μ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 array import array
try:
b = text.encode("latin1")
cnt = array('I', [0]) * 256
for x in b:
cnt[x] += 1
freqs = array('I')
for c in cnt:
if c:
freqs.append(c)
except:
counts = {}
get = counts.get
for ch in text:
counts[ch] = get(ch, 0) + 1
vals = counts.values()
freqs = array('I', vals)
m = len(freqs)
if m == 1:
return {"encoded_length": n, "decoded": text}
if m == 2:
return {"encoded_length": freqs[0] + freqs[1], "decoded": text}
freqs = array('I', sorted(freqs))
merged = array('I', [0]) * (m - 1)
i = j = k = 0
total = 0
while k < m - 1:
if j >= k:
a = freqs[i]
i += 1
elif i >= m:
a = merged[j]
j += 1
elif freqs[i] <= merged[j]:
a = freqs[i]
i += 1
else:
a = merged[j]
j += 1
if j >= k:
b2 = freqs[i]
i += 1
elif i >= m:
b2 = merged[j]
j += 1
elif freqs[i] <= merged[j]:
b2 = freqs[i]
i += 1
else:
b2 = merged[j]
j += 1
s = a + b2
merged[k] = s
total += s
k += 1
return {"encoded_length": total, "decoded": text}Score Difference
Tied
Runtime Advantage
60μs slower
Code Size
71 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 array import array | 7 | counts = {} |
| 8 | 8 | get = counts.get | |
| 9 | try: | 9 | for ch in text: |
| 10 | b = text.encode("latin1") | 10 | counts[ch] = get(ch, 0) + 1 |
| 11 | cnt = array('I', [0]) * 256 | 11 | |
| 12 | for x in b: | 12 | freqs = list(counts.values()) |
| 13 | cnt[x] += 1 | 13 | m = len(freqs) |
| 14 | 14 | ||
| 15 | freqs = array('I') | 15 | if m == 1: |
| 16 | for c in cnt: | 16 | return {"encoded_length": n, "decoded": text} |
| 17 | if c: | 17 | |
| 18 | freqs.append(c) | 18 | freqs.sort() |
| 19 | except: | 19 | |
| 20 | counts = {} | 20 | # Bottom-up two-queue Huffman merge: |
| 21 | get = counts.get | 21 | # use freqs as first queue and merged as second queue. |
| 22 | for ch in text: | 22 | merged = [0] * (m - 1) |
| 23 | counts[ch] = get(ch, 0) + 1 | 23 | i = j = k = 0 |
| 24 | vals = counts.values() | 24 | total = 0 |
| 25 | freqs = array('I', vals) | 25 | |
| 26 | 26 | while k < m - 1: | |
| 27 | m = len(freqs) | 27 | if i < m and (j >= k or freqs[i] <= merged[j]): |
| 28 | if m == 1: | 28 | a = freqs[i] |
| 29 | return {"encoded_length": n, "decoded": text} | 29 | i += 1 |
| 30 | if m == 2: | 30 | else: |
| 31 | return {"encoded_length": freqs[0] + freqs[1], "decoded": text} | 31 | a = merged[j] |
| 32 | 32 | j += 1 | |
| 33 | freqs = array('I', sorted(freqs)) | 33 | |
| 34 | merged = array('I', [0]) * (m - 1) | 34 | if i < m and (j >= k or freqs[i] <= merged[j]): |
| 35 | 35 | b = freqs[i] | |
| 36 | i = j = k = 0 | 36 | i += 1 |
| 37 | total = 0 | 37 | else: |
| 38 | 38 | b = merged[j] | |
| 39 | while k < m - 1: | 39 | j += 1 |
| 40 | if j >= k: | 40 | |
| 41 | a = freqs[i] | 41 | s = a + b |
| 42 | i += 1 | 42 | merged[k] = s |
| 43 | elif i >= m: | 43 | total += s |
| 44 | a = merged[j] | 44 | k += 1 |
| 45 | j += 1 | 45 | |
| 46 | elif freqs[i] <= merged[j]: | 46 | return {"encoded_length": total, "decoded": text} |
| 47 | a = freqs[i] | 47 | |
| 48 | i += 1 | 48 | |
| 49 | else: | 49 | |
| 50 | a = merged[j] | 50 | |
| 51 | j += 1 | 51 | |
| 52 | 52 | ||
| 53 | if j >= k: | 53 | |
| 54 | b2 = freqs[i] | 54 | |
| 55 | i += 1 | 55 | |
| 56 | elif i >= m: | 56 | |
| 57 | b2 = merged[j] | 57 | |
| 58 | j += 1 | 58 | |
| 59 | elif freqs[i] <= merged[j]: | 59 | |
| 60 | b2 = freqs[i] | 60 | |
| 61 | i += 1 | 61 | |
| 62 | else: | 62 | |
| 63 | b2 = merged[j] | 63 | |
| 64 | j += 1 | 64 | |
| 65 | 65 | ||
| 66 | s = a + b2 | 66 | |
| 67 | merged[k] = s | 67 | |
| 68 | total += s | 68 | |
| 69 | k += 1 | 69 | |
| 70 | 70 | ||
| 71 | return {"encoded_length": total, "decoded": text} | 71 |
1def solve(input):2 text = input.get("text", "")3 n = len(text)4 if n == 0:5 return {"encoded_length": 0, "decoded": ""}67 from array import array89 try:10 b = text.encode("latin1")11 cnt = array('I', [0]) * 25612 for x in b:13 cnt[x] += 11415 freqs = array('I')16 for c in cnt:17 if c:18 freqs.append(c)19 except:20 counts = {}21 get = counts.get22 for ch in text:23 counts[ch] = get(ch, 0) + 124 vals = counts.values()25 freqs = array('I', vals)2627 m = len(freqs)28 if m == 1:29 return {"encoded_length": n, "decoded": text}30 if m == 2:31 return {"encoded_length": freqs[0] + freqs[1], "decoded": text}3233 freqs = array('I', sorted(freqs))34 merged = array('I', [0]) * (m - 1)3536 i = j = k = 037 total = 03839 while k < m - 1:40 if j >= k:41 a = freqs[i]42 i += 143 elif i >= m:44 a = merged[j]45 j += 146 elif freqs[i] <= merged[j]:47 a = freqs[i]48 i += 149 else:50 a = merged[j]51 j += 15253 if j >= k:54 b2 = freqs[i]55 i += 156 elif i >= m:57 b2 = merged[j]58 j += 159 elif freqs[i] <= merged[j]:60 b2 = freqs[i]61 i += 162 else:63 b2 = merged[j]64 j += 16566 s = a + b267 merged[k] = s68 total += s69 k += 17071 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}