Solution #d720f0bf-a6c8-49aa-aae0-4cb9c0718e07
completedScore
100% (5/5)
Runtime
87μs
Delta
No change vs parent
Tied for best
Same as parent
Score
100% (5/5)
Runtime
87μ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:
counts = array('I', [0]) * 256
for ch in text:
counts[ord(ch)] += 1
freqs = array('I')
for c in counts:
if c:
freqs.append(c)
except:
d = {}
for ch in text:
d[ch] = d.get(ch, 0) + 1
freqs = array('I', d.values())
m = len(freqs)
if m == 1:
return {"encoded_length": n, "decoded": text}
freqs = array('I', sorted(freqs))
merged = array('I', [0]) * (m - 1)
i = 0
j = 0
total = 0
for k in range(m - 1):
if i < m and (j >= k or freqs[i] <= merged[j]):
a = freqs[i]
i += 1
else:
a = merged[j]
j += 1
if i < m and (j >= k or freqs[i] <= merged[j]):
b = freqs[i]
i += 1
else:
b = merged[j]
j += 1
s = a + b
merged[k] = s
total += s
return {"encoded_length": total, "decoded": text}Score Difference
Tied
Runtime Advantage
63μs slower
Code Size
54 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 | counts = array('I', [0]) * 256 | 10 | counts[ch] = get(ch, 0) + 1 |
| 11 | for ch in text: | 11 | |
| 12 | counts[ord(ch)] += 1 | 12 | freqs = list(counts.values()) |
| 13 | 13 | m = len(freqs) | |
| 14 | freqs = array('I') | 14 | |
| 15 | for c in counts: | 15 | if m == 1: |
| 16 | if c: | 16 | return {"encoded_length": n, "decoded": text} |
| 17 | freqs.append(c) | 17 | |
| 18 | except: | 18 | freqs.sort() |
| 19 | d = {} | 19 | |
| 20 | for ch in text: | 20 | # Bottom-up two-queue Huffman merge: |
| 21 | d[ch] = d.get(ch, 0) + 1 | 21 | # use freqs as first queue and merged as second queue. |
| 22 | freqs = array('I', d.values()) | 22 | merged = [0] * (m - 1) |
| 23 | 23 | i = j = k = 0 | |
| 24 | m = len(freqs) | 24 | total = 0 |
| 25 | if m == 1: | 25 | |
| 26 | return {"encoded_length": n, "decoded": text} | 26 | while k < m - 1: |
| 27 | 27 | if i < m and (j >= k or freqs[i] <= merged[j]): | |
| 28 | freqs = array('I', sorted(freqs)) | 28 | a = freqs[i] |
| 29 | merged = array('I', [0]) * (m - 1) | 29 | i += 1 |
| 30 | 30 | else: | |
| 31 | i = 0 | 31 | a = merged[j] |
| 32 | j = 0 | 32 | j += 1 |
| 33 | total = 0 | 33 | |
| 34 | 34 | if i < m and (j >= k or freqs[i] <= merged[j]): | |
| 35 | for k in range(m - 1): | 35 | b = freqs[i] |
| 36 | if i < m and (j >= k or freqs[i] <= merged[j]): | 36 | i += 1 |
| 37 | a = freqs[i] | 37 | else: |
| 38 | i += 1 | 38 | b = merged[j] |
| 39 | else: | 39 | j += 1 |
| 40 | a = merged[j] | 40 | |
| 41 | j += 1 | 41 | s = a + b |
| 42 | 42 | merged[k] = s | |
| 43 | if i < m and (j >= k or freqs[i] <= merged[j]): | 43 | total += s |
| 44 | b = freqs[i] | 44 | k += 1 |
| 45 | i += 1 | 45 | |
| 46 | else: | 46 | return {"encoded_length": total, "decoded": text} |
| 47 | b = merged[j] | 47 | |
| 48 | j += 1 | 48 | |
| 49 | 49 | ||
| 50 | s = a + b | 50 | |
| 51 | merged[k] = s | 51 | |
| 52 | total += s | 52 | |
| 53 | 53 | ||
| 54 | return {"encoded_length": total, "decoded": text} | 54 |
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 counts = array('I', [0]) * 25611 for ch in text:12 counts[ord(ch)] += 11314 freqs = array('I')15 for c in counts:16 if c:17 freqs.append(c)18 except:19 d = {}20 for ch in text:21 d[ch] = d.get(ch, 0) + 122 freqs = array('I', d.values())2324 m = len(freqs)25 if m == 1:26 return {"encoded_length": n, "decoded": text}2728 freqs = array('I', sorted(freqs))29 merged = array('I', [0]) * (m - 1)3031 i = 032 j = 033 total = 03435 for k in range(m - 1):36 if i < m and (j >= k or freqs[i] <= merged[j]):37 a = freqs[i]38 i += 139 else:40 a = merged[j]41 j += 14243 if i < m and (j >= k or freqs[i] <= merged[j]):44 b = freqs[i]45 i += 146 else:47 b = merged[j]48 j += 14950 s = a + b51 merged[k] = s52 total += s5354 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}