Solution #06ab1c25-28af-4434-8eff-fb20d9e0838f
completedScore
100% (5/5)
Runtime
115μs
Delta
No change vs parent
Tied for best
Same as parent
Score
100% (5/5)
Runtime
115μ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 = {}
get = counts.get
for ch in text:
counts[ch] = get(ch, 0) + 1
vals = tuple(counts.values())
if len(vals) == 1:
return {"encoded_length": n, "decoded": text}
s = ",".join(map(str, vals))
nums = sorted(map(int, s.split(",")))
i = 0
j = 0
q = ""
qlen = 0
total = 0
m = len(nums)
def take():
nonlocal i, j, q, qlen
if i < m:
if j < qlen:
k = q.find(",", j)
qv = int(q[j:k])
if nums[i] <= qv:
v = nums[i]
i += 1
return v
j = k + 1
return qv
v = nums[i]
i += 1
return v
k = q.find(",", j)
v = int(q[j:k])
j = k + 1
return v
while (m - i) + (qlen - j) // 2 > 1:
a = take()
b = take()
s2 = a + b
total += s2
q += str(s2) + ","
qlen = len(q)
return {"encoded_length": total, "decoded": text}Score Difference
Tied
Runtime Advantage
91μ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 | counts = {} | 7 | counts = {} |
| 8 | get = counts.get | 8 | get = counts.get |
| 9 | for ch in text: | 9 | for ch in text: |
| 10 | counts[ch] = get(ch, 0) + 1 | 10 | counts[ch] = get(ch, 0) + 1 |
| 11 | 11 | ||
| 12 | vals = tuple(counts.values()) | 12 | freqs = list(counts.values()) |
| 13 | if len(vals) == 1: | 13 | m = len(freqs) |
| 14 | return {"encoded_length": n, "decoded": text} | 14 | |
| 15 | 15 | if m == 1: | |
| 16 | s = ",".join(map(str, vals)) | 16 | return {"encoded_length": n, "decoded": text} |
| 17 | nums = sorted(map(int, s.split(","))) | 17 | |
| 18 | 18 | freqs.sort() | |
| 19 | i = 0 | 19 | |
| 20 | j = 0 | 20 | # Bottom-up two-queue Huffman merge: |
| 21 | q = "" | 21 | # use freqs as first queue and merged as second queue. |
| 22 | qlen = 0 | 22 | merged = [0] * (m - 1) |
| 23 | total = 0 | 23 | i = j = k = 0 |
| 24 | m = len(nums) | 24 | total = 0 |
| 25 | 25 | ||
| 26 | def take(): | 26 | while k < m - 1: |
| 27 | nonlocal i, j, q, qlen | 27 | if i < m and (j >= k or freqs[i] <= merged[j]): |
| 28 | if i < m: | 28 | a = freqs[i] |
| 29 | if j < qlen: | 29 | i += 1 |
| 30 | k = q.find(",", j) | 30 | else: |
| 31 | qv = int(q[j:k]) | 31 | a = merged[j] |
| 32 | if nums[i] <= qv: | 32 | j += 1 |
| 33 | v = nums[i] | 33 | |
| 34 | i += 1 | 34 | if i < m and (j >= k or freqs[i] <= merged[j]): |
| 35 | return v | 35 | b = freqs[i] |
| 36 | j = k + 1 | 36 | i += 1 |
| 37 | return qv | 37 | else: |
| 38 | v = nums[i] | 38 | b = merged[j] |
| 39 | i += 1 | 39 | j += 1 |
| 40 | return v | 40 | |
| 41 | k = q.find(",", j) | 41 | s = a + b |
| 42 | v = int(q[j:k]) | 42 | merged[k] = s |
| 43 | j = k + 1 | 43 | total += s |
| 44 | return v | 44 | k += 1 |
| 45 | 45 | ||
| 46 | while (m - i) + (qlen - j) // 2 > 1: | 46 | return {"encoded_length": total, "decoded": text} |
| 47 | a = take() | 47 | |
| 48 | b = take() | 48 | |
| 49 | s2 = a + b | 49 | |
| 50 | total += s2 | 50 | |
| 51 | q += str(s2) + "," | 51 | |
| 52 | qlen = len(q) | 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 counts = {}8 get = counts.get9 for ch in text:10 counts[ch] = get(ch, 0) + 11112 vals = tuple(counts.values())13 if len(vals) == 1:14 return {"encoded_length": n, "decoded": text}1516 s = ",".join(map(str, vals))17 nums = sorted(map(int, s.split(",")))1819 i = 020 j = 021 q = ""22 qlen = 023 total = 024 m = len(nums)2526 def take():27 nonlocal i, j, q, qlen28 if i < m:29 if j < qlen:30 k = q.find(",", j)31 qv = int(q[j:k])32 if nums[i] <= qv:33 v = nums[i]34 i += 135 return v36 j = k + 137 return qv38 v = nums[i]39 i += 140 return v41 k = q.find(",", j)42 v = int(q[j:k])43 j = k + 144 return v4546 while (m - i) + (qlen - j) // 2 > 1:47 a = take()48 b = take()49 s2 = a + b50 total += s251 q += str(s2) + ","52 qlen = len(q)5354 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}