8.16 - hard - 61

作者: 健时总向乱中忙 | 来源:发表于2017-08-17 02:55 被阅读21次

312. Burst Balloons

这种题型被总结为区间DP,一般用从终点朝起点去考虑会容易些,然后解法是记忆化搜索。

class Solution(object):
    def maxCoins(self, nums):
        """
        :type nums: List[int]
        :rtype: int
        """
        nums = [1] + nums + [1]
        return self.search(nums, 1, len(nums)-2, set(), {})
        
    
    def search(self, nums, left, right, visited, m):
        if (left, right) in visited:
            return m[(left, right)]
        res = 0
        for k in range(left, right+1): # bomb k
            mid = nums[k]*nums[left-1]*nums[right+1]
            left_val = self.search(nums, left, k-1, visited, m)
            right_val = self.search(nums, k+1, right, visited, m)
            res = max(res, left_val+right_val+mid)
        
        visited.add((left, right))
        m[(left, right)] = res
        return res

相关文章

  • 8.16 - hard - 61

    312. Burst Balloons 这种题型被总结为区间DP,一般用从终点朝起点去考虑会容易些,然后解法是记忆...

  • 8.16 - hard - 59

    305. Number of Islands II 一道union-find的题目,这类题目只要找准谁是boss就...

  • 8.16 - hard - 58

    302. Smallest Rectangle Enclosing Black Pixels 这道题有点意思,其实...

  • 8.16 - hard - 60

    308. Range Sum Query 2D - Mutable 这题所谓的标准解法是用segment tree...

  • 姨妈来

    8.16姨妈来

  • 2018-08-31

    8.16 460 9.2 460 10.1 11.1

  • 19/09/2017

    Work hard,play hard,study hard, love hard.一个小姐姐跟我这么说。她说后两...

  • 英文书法练习~learning

    learning is sometimes hard, But hard is not impossible.学习...

  • 你辛苦了。

    Thanks for your hard work. 重读:hard

  • 他对我很不客气。

    He is very hard / on me. 重读:hard

网友评论

    本文标题:8.16 - hard - 61

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