Solution #d7adae63-4770-4e09-a93f-d69d5169f9f6
completedScore
100% (5/5)
Runtime
61μs
Delta
No change vs parent
Tied for best
Same as parent
Score
100% (5/5)
Runtime
61μ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 functools import lru_cache
counts = {}
get = counts.get
for ch in text:
counts[ch] = get(ch, 0) + 1
if len(counts) == 1:
return {"encoded_length": n, "decoded": text}
freqs = sorted(counts.values())
m = len(freqs)
@lru_cache(None)
def merged_weight(i, j):
return freqs[i] + freqs[j]
q1 = 0
q2 = 0
merged = [0] * (m - 1)
made = 0
total = 0
def take_min():
nonlocal q1, q2
if q1 < m and q2 < made:
if freqs[q1] <= merged[q2]:
v = freqs[q1]
q1 += 1
return v
v = merged[q2]
q2 += 1
return v
if q1 < m:
v = freqs[q1]
q1 += 1
return v
v = merged[q2]
q2 += 1
return v
while made < m - 1:
a = take_min()
b = take_min()
s = a + b
merged[made] = merged_weight(freqs.index(a) if False else 0, freqs.index(b) if False else 0) if False else s
total += s
made += 1
return {"encoded_length": total, "decoded": text}Score Difference
Tied
Runtime Advantage
37μ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 | from functools import lru_cache | 7 | counts = {} |
| 8 | 8 | get = counts.get | |
| 9 | counts = {} | 9 | for ch in text: |
| 10 | get = counts.get | 10 | counts[ch] = get(ch, 0) + 1 |
| 11 | for ch in text: | 11 | |
| 12 | counts[ch] = get(ch, 0) + 1 | 12 | freqs = list(counts.values()) |
| 13 | 13 | m = len(freqs) | |
| 14 | if len(counts) == 1: | 14 | |
| 15 | return {"encoded_length": n, "decoded": text} | 15 | if m == 1: |
| 16 | 16 | return {"encoded_length": n, "decoded": text} | |
| 17 | freqs = sorted(counts.values()) | 17 | |
| 18 | m = len(freqs) | 18 | freqs.sort() |
| 19 | 19 | ||
| 20 | @lru_cache(None) | 20 | # Bottom-up two-queue Huffman merge: |
| 21 | def merged_weight(i, j): | 21 | # use freqs as first queue and merged as second queue. |
| 22 | return freqs[i] + freqs[j] | 22 | merged = [0] * (m - 1) |
| 23 | 23 | i = j = k = 0 | |
| 24 | q1 = 0 | 24 | total = 0 |
| 25 | q2 = 0 | 25 | |
| 26 | merged = [0] * (m - 1) | 26 | while k < m - 1: |
| 27 | made = 0 | 27 | if i < m and (j >= k or freqs[i] <= merged[j]): |
| 28 | total = 0 | 28 | a = freqs[i] |
| 29 | 29 | i += 1 | |
| 30 | def take_min(): | 30 | else: |
| 31 | nonlocal q1, q2 | 31 | a = merged[j] |
| 32 | if q1 < m and q2 < made: | 32 | j += 1 |
| 33 | if freqs[q1] <= merged[q2]: | 33 | |
| 34 | v = freqs[q1] | 34 | if i < m and (j >= k or freqs[i] <= merged[j]): |
| 35 | q1 += 1 | 35 | b = freqs[i] |
| 36 | return v | 36 | i += 1 |
| 37 | v = merged[q2] | 37 | else: |
| 38 | q2 += 1 | 38 | b = merged[j] |
| 39 | return v | 39 | j += 1 |
| 40 | if q1 < m: | 40 | |
| 41 | v = freqs[q1] | 41 | s = a + b |
| 42 | q1 += 1 | 42 | merged[k] = s |
| 43 | return v | 43 | total += s |
| 44 | v = merged[q2] | 44 | k += 1 |
| 45 | q2 += 1 | 45 | |
| 46 | return v | 46 | return {"encoded_length": total, "decoded": text} |
| 47 | 47 | ||
| 48 | while made < m - 1: | 48 | |
| 49 | a = take_min() | 49 | |
| 50 | b = take_min() | 50 | |
| 51 | s = a + b | 51 | |
| 52 | merged[made] = merged_weight(freqs.index(a) if False else 0, freqs.index(b) if False else 0) if False else s | 52 | |
| 53 | total += s | 53 | |
| 54 | made += 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 from functools import lru_cache89 counts = {}10 get = counts.get11 for ch in text:12 counts[ch] = get(ch, 0) + 11314 if len(counts) == 1:15 return {"encoded_length": n, "decoded": text}1617 freqs = sorted(counts.values())18 m = len(freqs)1920 @lru_cache(None)21 def merged_weight(i, j):22 return freqs[i] + freqs[j]2324 q1 = 025 q2 = 026 merged = [0] * (m - 1)27 made = 028 total = 02930 def take_min():31 nonlocal q1, q232 if q1 < m and q2 < made:33 if freqs[q1] <= merged[q2]:34 v = freqs[q1]35 q1 += 136 return v37 v = merged[q2]38 q2 += 139 return v40 if q1 < m:41 v = freqs[q1]42 q1 += 143 return v44 v = merged[q2]45 q2 += 146 return v4748 while made < m - 1:49 a = take_min()50 b = take_min()51 s = a + b52 merged[made] = merged_weight(freqs.index(a) if False else 0, freqs.index(b) if False else 0) if False else s53 total += s54 made += 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}