美文网首页
375. Guess Number Higher or Lowe

375. Guess Number Higher or Lowe

作者: 阿团相信梦想都能实现 | 来源:发表于2016-12-16 05:40 被阅读0次
bottom-up dp solution 

class Solution(object):
    def getMoneyAmount(self, n):
        """
        :type n: int
        :rtype: int
        """
        dp=[[0]*(n+1) for _ in xrange(n+1)]
        for lo in xrange(n,0,-1):
            for hi in xrange(lo+1,n+1):
                dp[lo][hi]=min(x+max(dp[lo][x-1],dp[x+1][hi]) for x in xrange(lo,hi))
                
        return dp[1][n]
              
class Solution(object):
    def getMoneyAmount(self, n):
        """
        :type n: int
        :rtype: int
        """
        class helper(dict): 
            def __missing__(self,(lo,hi)):
                if lo>=hi:
                    return 0
                ret=self[lo,hi]=min(x+max(self[lo,x-1],self[x+1,hi]) for x in range(lo,hi))
                return ret
        return helper()[1,n]
                

相关文章

网友评论

      本文标题:375. Guess Number Higher or Lowe

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