Iam_Sandeep

Untitled

Mar 20th, 2026
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.77 KB | None | 0 0
  1. def Q2(errorString: str, x: int, y: int):
  2.     n = len(errorString)
  3.  
  4.     from functools import lru_cache
  5.  
  6.     @lru_cache()
  7.     def dp(i, zero_count):
  8.         if i == 0:
  9.             return 0
  10.  
  11.         one_count = (i - (zero_count - 1))
  12.  
  13.         if errorString[i] == '0':
  14.             return y * one_count + dp(i - 1, zero_count - 1)
  15.         elif errorString[i] == '1':
  16.             return x * zero_count + dp(i - 1, zero_count)
  17.         else:
  18.             return min(
  19.                 y * one_count + dp(i - 1, zero_count - 1),
  20.                 x * zero_count + dp(i - 1, zero_count)
  21.             )
  22.  
  23.     count = len([c for c in errorString if c == '?'])
  24.     zero_count = len([c for c in errorString if c == '0'])
  25.  
  26.     return min(dp(n - 1, zero_count + i) for i in range(count))
Advertisement
Add Comment
Please, Sign In to add comment