Solution #f9d7018b-4fd9-4d38-a260-8e9800fb243d
completedScore
100% (5/5)
Runtime
40μs
Delta
No change vs parent
Tied for best
Same as parent
Score
100% (5/5)
Runtime
40μ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": ""}
counts = {}
for ch in text:
counts[ch] = counts.get(ch, 0) + 1
freqs = list(counts.values())
m = len(freqs)
if m == 1:
return {"encoded_length": n, "decoded": text}
if m == 2:
return {"encoded_length": n, "decoded": text}
buckets = [0] * (n + 1)
for f in freqs:
buckets[f] += 1
q1 = [0] * m
t = 0
for f in range(1, n + 1):
c = buckets[f]
while c:
q1[t] = f
t += 1
c -= 1
q2 = [0] * (m - 1)
i = j = k = 0
total = 0
while k < m - 1:
if i < m and (j >= k or q1[i] <= q2[j]):
a = q1[i]
i += 1
else:
a = q2[j]
j += 1
if i < m and (j >= k or q1[i] <= q2[j]):
b = q1[i]
i += 1
else:
b = q2[j]
j += 1
s = a + b
q2[k] = s
total += s
k += 1
return {"encoded_length": total, "decoded": text}Score Difference
Tied
Runtime Advantage
16μs slower
Code Size
56 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 | counts = {} | 7 | counts = {} |
| 8 | for ch in text: | 8 | get = counts.get |
| 9 | counts[ch] = counts.get(ch, 0) + 1 | 9 | for ch in text: |
| 10 | 10 | counts[ch] = get(ch, 0) + 1 | |
| 11 | freqs = list(counts.values()) | 11 | |
| 12 | m = len(freqs) | 12 | freqs = list(counts.values()) |
| 13 | 13 | m = len(freqs) | |
| 14 | if m == 1: | 14 | |
| 15 | return {"encoded_length": n, "decoded": text} | 15 | if m == 1: |
| 16 | if m == 2: | 16 | return {"encoded_length": n, "decoded": text} |
| 17 | return {"encoded_length": n, "decoded": text} | 17 | |
| 18 | 18 | freqs.sort() | |
| 19 | buckets = [0] * (n + 1) | 19 | |
| 20 | for f in freqs: | 20 | # Bottom-up two-queue Huffman merge: |
| 21 | buckets[f] += 1 | 21 | # use freqs as first queue and merged as second queue. |
| 22 | 22 | merged = [0] * (m - 1) | |
| 23 | q1 = [0] * m | 23 | i = j = k = 0 |
| 24 | t = 0 | 24 | total = 0 |
| 25 | for f in range(1, n + 1): | 25 | |
| 26 | c = buckets[f] | 26 | while k < m - 1: |
| 27 | while c: | 27 | if i < m and (j >= k or freqs[i] <= merged[j]): |
| 28 | q1[t] = f | 28 | a = freqs[i] |
| 29 | t += 1 | 29 | i += 1 |
| 30 | c -= 1 | 30 | else: |
| 31 | 31 | a = merged[j] | |
| 32 | q2 = [0] * (m - 1) | 32 | j += 1 |
| 33 | i = j = k = 0 | 33 | |
| 34 | total = 0 | 34 | if i < m and (j >= k or freqs[i] <= merged[j]): |
| 35 | 35 | b = freqs[i] | |
| 36 | while k < m - 1: | 36 | i += 1 |
| 37 | if i < m and (j >= k or q1[i] <= q2[j]): | 37 | else: |
| 38 | a = q1[i] | 38 | b = merged[j] |
| 39 | i += 1 | 39 | j += 1 |
| 40 | else: | 40 | |
| 41 | a = q2[j] | 41 | s = a + b |
| 42 | j += 1 | 42 | merged[k] = s |
| 43 | 43 | total += s | |
| 44 | if i < m and (j >= k or q1[i] <= q2[j]): | 44 | k += 1 |
| 45 | b = q1[i] | 45 | |
| 46 | i += 1 | 46 | return {"encoded_length": total, "decoded": text} |
| 47 | else: | 47 | |
| 48 | b = q2[j] | 48 | |
| 49 | j += 1 | 49 | |
| 50 | 50 | ||
| 51 | s = a + b | 51 | |
| 52 | q2[k] = s | 52 | |
| 53 | total += s | 53 | |
| 54 | k += 1 | 54 | |
| 55 | 55 | ||
| 56 | return {"encoded_length": total, "decoded": text} | 56 |
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 for ch in text:9 counts[ch] = counts.get(ch, 0) + 11011 freqs = list(counts.values())12 m = len(freqs)1314 if m == 1:15 return {"encoded_length": n, "decoded": text}16 if m == 2:17 return {"encoded_length": n, "decoded": text}1819 buckets = [0] * (n + 1)20 for f in freqs:21 buckets[f] += 12223 q1 = [0] * m24 t = 025 for f in range(1, n + 1):26 c = buckets[f]27 while c:28 q1[t] = f29 t += 130 c -= 13132 q2 = [0] * (m - 1)33 i = j = k = 034 total = 03536 while k < m - 1:37 if i < m and (j >= k or q1[i] <= q2[j]):38 a = q1[i]39 i += 140 else:41 a = q2[j]42 j += 14344 if i < m and (j >= k or q1[i] <= q2[j]):45 b = q1[i]46 i += 147 else:48 b = q2[j]49 j += 15051 s = a + b52 q2[k] = s53 total += s54 k += 15556 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}