Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def Q2(errorString: str, x: int, y: int):
- n = len(errorString)
- from functools import lru_cache
- @lru_cache()
- def dp(i, zero_count):
- if i == 0:
- return 0
- one_count = (i - (zero_count - 1))
- if errorString[i] == '0':
- return y * one_count + dp(i - 1, zero_count - 1)
- elif errorString[i] == '1':
- return x * zero_count + dp(i - 1, zero_count)
- else:
- return min(
- y * one_count + dp(i - 1, zero_count - 1),
- x * zero_count + dp(i - 1, zero_count)
- )
- count = len([c for c in errorString if c == '?'])
- zero_count = len([c for c in errorString if c == '0'])
- return min(dp(n - 1, zero_count + i) for i in range(count))
Advertisement
Add Comment
Please, Sign In to add comment