|
| 1 | +from typing import List |
| 2 | + |
| 3 | + |
| 4 | +class Solution: |
| 5 | + def canPartition(self, nums: List[int]) -> bool: |
| 6 | + """ |
| 7 | + 1 5 11 5 |
| 8 | + 0 T T T F |
| 9 | + 1 T T T F |
| 10 | + 2 F F F F |
| 11 | + 3 F F F F |
| 12 | + 4 F F F F |
| 13 | + 5 F T T F |
| 14 | + 6 F T T F |
| 15 | + 7 F F F F |
| 16 | + 8 F F F F |
| 17 | + 9 F F F F |
| 18 | + 10 F F F F |
| 19 | + 11 F F T F |
| 20 | + |
| 21 | + dp[row][col]: 表示前col个元素是否可以构成row这个数字 |
| 22 | + dp[row][col] = dp[row][col-1] or dp[row-nums[col]][col-1] |
| 23 | + 状态压缩: col 只与 col-1 有关 |
| 24 | + dp[row] = dp[row] or dp[row-nums[col]], row 从后往前, 从前往后不行是因为 col 会覆盖 col-1 |
| 25 | + """ |
| 26 | + total = sum(nums) |
| 27 | + if total % 2 > 0 or len(nums) <= 1: |
| 28 | + return False |
| 29 | + target = total // 2 |
| 30 | + dp = [False for _ in range(target + 1)] |
| 31 | + dp[0] = True |
| 32 | + if nums[0] <= target: |
| 33 | + dp[nums[0]] = True |
| 34 | + for col in range(1, len(nums)): |
| 35 | + for row in range(target, 0, -1): |
| 36 | + dp[row] = dp[row] or (row >= nums[col] and dp[row - nums[col]]) |
| 37 | + if dp[target] is True: |
| 38 | + return True |
| 39 | + return False |
| 40 | + |
| 41 | + |
| 42 | +# class Solution: |
| 43 | +# def canPartition(self, nums: List[int]) -> bool: |
| 44 | +# """ |
| 45 | +# 1 5 11 5 |
| 46 | +# 0 T T T F |
| 47 | +# 1 T T T F |
| 48 | +# 2 F F F F |
| 49 | +# 3 F F F F |
| 50 | +# 4 F F F F |
| 51 | +# 5 F T T F |
| 52 | +# 6 F T T F |
| 53 | +# 7 F F F F |
| 54 | +# 8 F F F F |
| 55 | +# 9 F F F F |
| 56 | +# 10 F F F F |
| 57 | +# 11 F F T F |
| 58 | + |
| 59 | +# dp[row][col]: 表示前col个元素是否可以构成row这个数字 |
| 60 | +# dp[row][col] = dp[row][col-1] or dp[row-nums[col]][col-1] |
| 61 | +# """ |
| 62 | +# total = sum(nums) |
| 63 | +# if total % 2 > 0 or len(nums) <= 1: |
| 64 | +# return False |
| 65 | +# target = total // 2 |
| 66 | +# dp = [[False for _ in nums] for _ in range(total + 1)] |
| 67 | +# dp[0][0] = dp[nums[0]][0] = True |
| 68 | +# for col in range(1, len(nums)): |
| 69 | +# for row in range(target + 1): |
| 70 | +# dp[row][col] = dp[row][col - 1] or (row >= nums[col] and dp[row - nums[col]][col - 1]) |
| 71 | +# if dp[target][col] is True: |
| 72 | +# return True |
| 73 | +# return False |
| 74 | + |
| 75 | + |
| 76 | +# class Solution: |
| 77 | +# def canPartition(self, nums: List[int]) -> bool: |
| 78 | +# """ |
| 79 | +# (11,0) |
| 80 | +# (10,1) (11,1) |
| 81 | +# (5,2) (10,2) (6,2) (11,2) |
| 82 | +# - (5,3) - (6,3) (0,3) (11,3) |
| 83 | +# (0,4) |
| 84 | +# 超时的原因在于重复计数子问题 2**N |
| 85 | +# 记录 (target, index) 的解避免重复计算 |
| 86 | +# """ |
| 87 | +# total = sum(nums) |
| 88 | +# if total % 2 > 0 or len(nums) <= 1: |
| 89 | +# return False |
| 90 | +# memory = dict() |
| 91 | +# return self.search(nums, total // 2, 0, memory) |
| 92 | + |
| 93 | +# def search(self, nums: List[int], target: int, index: int, memory: dict) -> bool: |
| 94 | +# if target == 0: |
| 95 | +# return True |
| 96 | +# if target < 0: |
| 97 | +# return False |
| 98 | +# if index >= len(nums): |
| 99 | +# return False |
| 100 | +# if memory.get((target, index)) in (True, False): |
| 101 | +# return memory[(target, index)] |
| 102 | +# got = self.search(nums, target - nums[index], index + 1, memory) |
| 103 | +# memory[(target - nums[index], index + 1)] = got |
| 104 | +# not_got = self.search(nums, target, index + 1, memory) |
| 105 | +# memory[(target, index + 1)] = not_got |
| 106 | +# return got or not_got |
| 107 | + |
| 108 | + |
| 109 | +# class Solution: |
| 110 | +# def canPartition(self, nums: List[int]) -> bool: |
| 111 | +# # 暴力搜索 Timeout |
| 112 | +# total = sum(nums) |
| 113 | +# if total % 2 > 0 or len(nums) <= 1: |
| 114 | +# return False |
| 115 | +# nums.sort() |
| 116 | +# return self.search(nums, total // 2, 0) |
| 117 | + |
| 118 | +# def search(self, nums: List[int], target: int, index: int): |
| 119 | +# if target == 0: |
| 120 | +# return True |
| 121 | +# if target < 0: |
| 122 | +# return False |
| 123 | +# if index >= len(nums): |
| 124 | +# return False |
| 125 | +# return self.search(nums, target - nums[index], index + 1) or self.search(nums, target, index + 1) |
| 126 | + |
| 127 | + |
| 128 | +if __name__ == "__main__": |
| 129 | + # assert Solution().canPartition(nums=[1, 5, 11, 5]) is True |
| 130 | + # assert Solution().canPartition(nums=[1, 2, 3, 5]) is False |
| 131 | + # assert Solution().canPartition(nums=[9, 5]) is False |
| 132 | + assert Solution().canPartition(nums=[14, 9, 8, 4, 3, 2]) is True |
0 commit comments