Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import functools
- class Solution:
- # @param A : string
- # @param B : integer
- # @return an integer
- @functools.lru_cache(None)
- def dfs(self,i,d,A) :
- n=len(A)
- if d==1:
- w,b=0,0
- for idx in range(i,n):
- w+=A[idx]=='W'
- b = n-i-w
- return w*b
- res,w,b= float('inf'), 0,0
- for j in range(i,n):
- w+=A[j]=='W'
- b+=A[j]=='B'
- res = min(res,w*b+self.dfs(j+1,d-1,A))
- return res
- def arrange(self, A, B):
- n = len(A)
- if n < B:
- return -1
- return self.dfs(0,B,A)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement