10038. Maximize the Number of Partitions After Operations

 

10038. Maximize the Number of Partitions After Operations

class Solution:
    def maxPartitionsAfterOperations(self, s: str, k: int) -> int:
        s=[ord(c)-ord('a') for c in s]
        @cache
        def dp(idx,left,seen):
            if idx==len(s):return 0

            ss=seen | (1<k:
                ret=1+dp(idx+1,left,1<k:
                        ret=max(ret,1+dp(idx+1,left-1,1<

1<

这里@cache相当于直接数组dp了

最后+1是因为最后一个划分一直没有算上,注意bc>k的时候之前才进行了计算

你可能感兴趣的:(leetcode)