美文网首页
Leetcode 964.Least Operators to

Leetcode 964.Least Operators to

作者: lee_5a30 | 来源:发表于2018-12-23 12:29 被阅读0次

Well, we can also solve in dp format.
Python3

    @functools.lru_cache(None)
    def leastOpsExpressTarget(self, x, y):
        k = int(math.log(y, x)) + 1
        def dfs(y, k):
            if k == 0 or y == 0:
                return y + y - 1
            need, left = divmod(y, x**k)
            return min(dfs(left, k - 1) + need * k, dfs(x**k - left, k - 1) + (need + 1) * k)
        return dfs(y, k)

相关文章

网友评论

      本文标题:Leetcode 964.Least Operators to

      本文链接:https://www.haomeiwen.com/subject/jntnkqtx.html