Solution #42ace40a-597d-41fc-8f8a-936bdbd55128
completedScore
40% (2/5)
Runtime
62μs
Delta
-60.0% vs parent
-60.0% vs best
Regression from parent
Score
40% (2/5)
Runtime
62μs
Delta
-60.0% vs parent
-60.0% vs best
Regression from parent
def solve(input):
text = input.get("text", "")
n = len(text)
if not n:
return {"encoded_length": 0, "decoded": ""}
counts = {}
_ = [counts.__setitem__(ch, counts.get(ch, 0) + 1) for ch in text]
vals = sorted(counts.values())
k = len(vals)
if k == 1:
return {"encoded_length": n, "decoded": text}
q = vals + [0] * (k - 1)
i = 0
j = k
w = 0
total = 0
for t in range(k - 1):
a = q[i] if i < j and (j > k + t - 1 or q[i] <= q[j]) else q[j]
i += 1 if i < j and (j > k + t - 1 or q[i] <= q[j]) else 0
j += 0 if i <= j else 0
if not (i - 1 < j and (j > k + t - 1 or q[i - 1] <= q[j])):
j += 1
b = q[i] if i < j and (j > k + t - 1 or q[i] <= q[j]) else q[j]
i += 1 if i < j and (j > k + t - 1 or q[i] <= q[j]) else 0
if not (i - 1 < j and (j > k + t - 1 or q[i - 1] <= q[j])):
j += 1
s = a + b
q[k + w] = s
w += 1
total += s
return {"encoded_length": total, "decoded": text}Score Difference
-60.0%
Runtime Advantage
38μs slower
Code Size
37 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 not n: | 4 | if n == 0: |
| 5 | return {"encoded_length": 0, "decoded": ""} | 5 | return {"encoded_length": 0, "decoded": ""} |
| 6 | 6 | ||
| 7 | counts = {} | 7 | counts = {} |
| 8 | _ = [counts.__setitem__(ch, counts.get(ch, 0) + 1) for ch in text] | 8 | get = counts.get |
| 9 | vals = sorted(counts.values()) | 9 | for ch in text: |
| 10 | k = len(vals) | 10 | counts[ch] = get(ch, 0) + 1 |
| 11 | if k == 1: | 11 | |
| 12 | return {"encoded_length": n, "decoded": text} | 12 | freqs = list(counts.values()) |
| 13 | 13 | m = len(freqs) | |
| 14 | q = vals + [0] * (k - 1) | 14 | |
| 15 | i = 0 | 15 | if m == 1: |
| 16 | j = k | 16 | return {"encoded_length": n, "decoded": text} |
| 17 | w = 0 | 17 | |
| 18 | total = 0 | 18 | freqs.sort() |
| 19 | 19 | ||
| 20 | for t in range(k - 1): | 20 | # Bottom-up two-queue Huffman merge: |
| 21 | a = q[i] if i < j and (j > k + t - 1 or q[i] <= q[j]) else q[j] | 21 | # use freqs as first queue and merged as second queue. |
| 22 | i += 1 if i < j and (j > k + t - 1 or q[i] <= q[j]) else 0 | 22 | merged = [0] * (m - 1) |
| 23 | j += 0 if i <= j else 0 | 23 | i = j = k = 0 |
| 24 | if not (i - 1 < j and (j > k + t - 1 or q[i - 1] <= q[j])): | 24 | total = 0 |
| 25 | j += 1 | 25 | |
| 26 | 26 | while k < m - 1: | |
| 27 | b = q[i] if i < j and (j > k + t - 1 or q[i] <= q[j]) else q[j] | 27 | if i < m and (j >= k or freqs[i] <= merged[j]): |
| 28 | i += 1 if i < j and (j > k + t - 1 or q[i] <= q[j]) else 0 | 28 | a = freqs[i] |
| 29 | if not (i - 1 < j and (j > k + t - 1 or q[i - 1] <= q[j])): | 29 | i += 1 |
| 30 | j += 1 | 30 | else: |
| 31 | 31 | a = merged[j] | |
| 32 | s = a + b | 32 | j += 1 |
| 33 | q[k + w] = s | 33 | |
| 34 | w += 1 | 34 | if i < m and (j >= k or freqs[i] <= merged[j]): |
| 35 | total += s | 35 | b = freqs[i] |
| 36 | 36 | i += 1 | |
| 37 | return {"encoded_length": total, "decoded": text} | 37 | else: |
| 38 | 38 | b = merged[j] | |
| 39 | 39 | j += 1 | |
| 40 | 40 | ||
| 41 | 41 | s = a + b | |
| 42 | 42 | merged[k] = s | |
| 43 | 43 | total += s | |
| 44 | 44 | k += 1 | |
| 45 | 45 | ||
| 46 | 46 | return {"encoded_length": total, "decoded": text} |
1def solve(input):2 text = input.get("text", "")3 n = len(text)4 if not n:5 return {"encoded_length": 0, "decoded": ""}67 counts = {}8 _ = [counts.__setitem__(ch, counts.get(ch, 0) + 1) for ch in text]9 vals = sorted(counts.values())10 k = len(vals)11 if k == 1:12 return {"encoded_length": n, "decoded": text}1314 q = vals + [0] * (k - 1)15 i = 016 j = k17 w = 018 total = 01920 for t in range(k - 1):21 a = q[i] if i < j and (j > k + t - 1 or q[i] <= q[j]) else q[j]22 i += 1 if i < j and (j > k + t - 1 or q[i] <= q[j]) else 023 j += 0 if i <= j else 024 if not (i - 1 < j and (j > k + t - 1 or q[i - 1] <= q[j])):25 j += 12627 b = q[i] if i < j and (j > k + t - 1 or q[i] <= q[j]) else q[j]28 i += 1 if i < j and (j > k + t - 1 or q[i] <= q[j]) else 029 if not (i - 1 < j and (j > k + t - 1 or q[i - 1] <= q[j])):30 j += 13132 s = a + b33 q[k + w] = s34 w += 135 total += s3637 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}