Solution #7eba7d1a-1fe6-4b13-9a1c-45ff8c9b0834
failedScore
0% (0/5)
Runtime
777μs
Delta
-100.0% vs parent
-100.0% vs best
Regression from parent
Score
0% (0/5)
Runtime
777μs
Delta
-100.0% vs parent
-100.0% vs best
Regression from parent
def solve(input):
data = input.get("data", "")
if not isinstance(data, str) or not data:
return 999.0
# Approach: LZ77 with memoization and recursion
def compress_recursively(s, i, memo):
if i >= len(s):
return []
if i in memo:
return memo[i]
best_match_length = 0
best_match_position = 0
for j in range(max(0, i - 255), i):
match_length = 0
while (i + match_length < len(s)) and (s[j + match_length] == s[i + match_length]):
match_length += 1
if match_length > best_match_length:
best_match_length = match_length
best_match_position = i - j
if best_match_length > 0:
result = [(best_match_position, best_match_length, s[i + best_match_length])] + compress_recursively(s, i + best_match_length + 1, memo)
else:
result = [(0, 0, s[i])] + compress_recursively(s, i + 1, memo)
memo[i] = result
return result
def decompress_recursively(compressed_data, i, decompressed, memo):
if i >= len(compressed_data):
return decompressed
if i in memo:
return memo[i]
position, length, next_char = compressed_data[i]
if position > 0:
start = len(decompressed) - position
for j in range(length):
decompressed += decompressed[start + j]
decompressed += next_char
memo[i] = decompress_recursively(compressed_data, i + 1, decompressed, memo)
return memo[i]
if len(data) == 0:
return 999.0
compressed_data = compress_recursively(data, 0, {})
decompressed_data = decompress_recursively(compressed_data, 0, "", {})
if decompressed_data != data:
return 999.0
original_size = len(data) * 8 # assuming each char is 1 byte (8 bits)
compressed_size = sum(8 + 8 + 8 for _ in compressed_data) # 8 bits each for position, length, and next_char
if original_size == 0:
return 999.0
compression_ratio = compressed_size / original_size
return 1.0 - compression_ratio