left和 + right和 = sums
left和 - right和 = target
left和 = (sums + target) / 2
其中,target,sums确定
dp[j] = 和为j的方法数
sums = sum(nums)
left = (sums + target) / 2
if left % 2: return 0
if(abs(s) > sums): return 0
dp = [0] * (sums + 1)
dp[0] = 1
for num in nums:
for j in range(sums, num-1, -1):
dp[j] += dp[j-num]
return dp[left]
code
py复制代码
class Solution:
def findTargetSumWays(self, nums: List[int], target: int) -> int:
total_sum = sum(nums) # 计算nums的总和
if abs(target) > total_sum:
return 0 # 此时没有方案
if (target + total_sum) % 2 == 1:
return 0 # 此时没有方案
target_sum = (target + total_sum) // 2 # 目标和
dp = [0] * (target_sum + 1) # 创建动态规划数组,初始化为0
dp[0] = 1 # 当目标和为0时,只有一种方案,即什么都不选
for num in nums:
for j in range(target_sum, num - 1, -1):
dp[j] += dp[j - num] # 状态转移方程,累加不同选择方式的数量
return dp[target_sum] # 返回达到目标和的方案数
回溯方法
py复制代码
class Solution:
def backtracking(self, candidates, target, total, startIndex, path, result):
if total == target:
result.append(path[:]) # 将当前路径的副本添加到结果中
# 如果 sum + candidates[i] > target,则停止遍历
for i in range(startIndex, len(candidates)):
if total + candidates[i] > target:
break
total += candidates[i]
path.append(candidates[i])
self.backtracking(candidates, target, total, i + 1, path, result)
total -= candidates[i]
path.pop()
def findTargetSumWays(self, nums: List[int], target: int) -> int:
total = sum(nums)
if target > total:
return 0 # 此时没有方案
if (target + total) % 2 != 0:
return 0 # 此时没有方案,两个整数相加时要注意数值溢出的问题
bagSize = (target + total) // 2 # 转化为组合总和问题,bagSize就是目标和
# 以下是回溯法代码
result = []
nums.sort() # 需要对nums进行排序
self.backtracking(nums, bagSize, 0, 0, [], result)
return len(result)
474.一和零
py复制代码
给你一个二进制字符串数组 strs 和两个整数 m 和 n 。
请你找出并返回 strs 的最大子集的大小,该子集中 最多 有 m 个 0 和 n 个 1 。
如果 x 的所有元素也是 y 的元素,集合 x 是集合 y 的 子集 。
示例 1:
输入:strs = ["10", "0001", "111001", "1", "0"], m = 5, n = 3
输出:4
解释:最多有 5 个 0 和 3 个 1 的最大子集是 {"10","0001","1","0"} ,因此答案是 4 。 其他满足题意但较小的子集包括 {"0001","1"} 和 {"10","1","0"} 。{"111001"} 不满足题意,因为它含 4 个 1 ,大于 n 的值 3 。
示例 2:
输入:strs = ["10", "0", "1"], m = 1, n = 1
输出:2
解释:最大的子集是 {"0", "1"} ,所以答案是 2 。
提示:
1 <= strs.length <= 600
1 <= strs[i].length <= 100
strs[i] 仅由 '0' 和 '1' 组成
1 <= m, n <= 100
思路
py复制代码
dp[i][j] 0的最大个数<=i, 1的最大个数<=j的字符串最大个数为dp[i][j]
dp[i][j]= max(dp[i][j], dp[i-cur0][j-cur1] + 1)
dp[j] = max(dp[j], dp[j-weigh[i]] + value[i])
dp[i][j] = [[0] * (n + 1)] * (m + 1) # m = 0, n = 1
dp[0][0] = 1
strs = ["10", "0001", "111001", "1", "0"]
v = "10"
cur0, cur1 = 1,1
dp[5][3]
for i in range(5, -1):
forj in range(3, -1,-1):
if i >= 1 and j >= 1:
dp[i][j] = 1
v = "0001"
cur0, cur1 = 3, 1
for i in range(5, -1):
forj in range(3, -1,-1):
if i >= 3 and j >= 1:
dp[i][j] = max(dp[i][j], dp[i-3][j-1] + 1)
v = "111001"
cur0, cur1 = 2, 4
for i in range(5, -1):
forj in range(3, -1,-1):
if i >= 2 and j >= 4:
dp[i][j] = max(dp[i][j], dp[i-2][j-4] + 1)
v = "1"
cur0, cur1 = 0, 1
for i in range(5, -1):
forj in range(3, -1,-1):
if i >= 0 and j >= 1:
dp[i][j] = max(dp[i][j], dp[i-0][j-1] + 1)
v = "0"
cur0, cur1 = 1, 0
for i in range(5, -1):
forj in range(3, -1,-1):
if i >= 1 and j >= 0:
dp[i][j] = max(dp[i][j], dp[i-1][j-0] + 1)
def counts1(str1):
cur0, cur1 = 0, 0
for v in str1:
if v == '1': cur1 += 1
else: cur0 += 1
return cur0, cur1
for v in strs:
cur0, cur1 = counts1(v)
for i in range(n, -1, -1):
for j in range(m, -1, -1):
if i >= cur0 and j >= cur1:
dp[i][j] = max(dp[i][j], dp[i-cur0][j-cur1] + 1)