[LeetCode]Matchsticks to Square

题目描述:

LeetCode 473. Matchsticks to Square

Remember the story of Little Match Girl? By now, you know exactly what matchsticks the little match girl has, please find out a way you can make one square by using up all those matchsticks. You should not break any stick, but you can link them up, and each matchstick must be used exactly one time.

Your input will be several matchsticks the girl has, represented with their stick length. Your output will either be true or false, to represent whether you can save this little girl or not.

Example 1:

Input: [1,1,2,2,2]
Output: true

Explanation: You can form a square with length 2, one side of the square came two sticks with length 1.

Example 2:

Input: [3,3,3,3,4]
Output: false

Explanation: You cannot find a way to form a square with all the matchsticks.

Note:

  1. The length sum of the given matchsticks is in the range of 0 to 10^9.
  2. The length of the given matchstick array will not exceed 15.

题目大意:

给定一些整数,代表火柴棍的长度。求这些火柴棍是否可以组成一个正方形。火柴棍不可以拆分,但是可以拼接。

注意:

  1. 火柴棍的长度之和在[0, 10^9]之间。
  2. 火柴棍数组的大小不会超过15。

解题思路:

记忆化搜索

利用辅助数组dp[i][j]记录是否可以用火柴棍数组i,拼接成j条长度相等的边。

Python代码:

class Solution(object):
    def makesquare(self, nums):
        """
        :type nums: List[int]
        :rtype: bool
        """
        if sum(nums) % 4: return False
        self.q = sum(nums) / 4
        self.dp = collections.defaultdict(dict)
        return self.solve(nums, 4)
        
    def solve(self, nums, part):
        total = sum(nums)
        if part == 1:
            return total == self.q
        size = len(nums)
        if size < part or total % part:
            return False
        tnums = tuple(nums)
        if part in self.dp[tnums]:
            return self.dp[tnums][part]
        for x in range(1, (1 << size) - 1):
            left, right = [], []
            for y in range(size):
                if x & (1 << y):
                    left.append(nums[y])
                else:
                    right.append(nums[y])
            if sum(left) != self.q:
                continue
            if self.solve(right, part - 1):
                self.dp[tnums][part] = True
                return True
        self.dp[tnums][part] = False
        return False

 

本文链接:http://bookshadow.com/weblog/2016/12/18/leetcode-matchsticks-to-square/
请尊重作者的劳动成果,转载请注明出处!书影博客保留对文章的所有权利。

如果您喜欢这篇博文,欢迎您捐赠书影博客: ,查看支付宝二维码

Pingbacks已关闭。

暂无评论

张贴您的评论