• 代码随想录算法训练营Day43 | 动态规划(5/17) LeetCode 1049. 最后一块石头的重量 II 494. 目标和 474.一和零


    动态规划第五天,加油!

    第一题

    1049. Last Stone Weight II

    You are given an array of integers stones where stones[i] is the weight of the ith stone.

    We are playing a game with the stones. On each turn, we choose any two stones and smash them together. Suppose the stones have weights x and y with x <= y. The result of this smash is:

    • If x == y, both stones are destroyed, and
    • If x != y, the stone of weight x is destroyed, and the stone of weight y has new weight y - x.

    At the end of the game, there is at most one stone left.

    Return the smallest possible weight of the left stone. If there are no stones left, return 0.

    本题其实就是尽量让石头分成重量相同的两堆,相撞之后剩下的石头最小,这样就化解成01背包问题了。

    本题物品的重量为stones[i],物品的价值也为stones[i]。对应着01背包里的物品重量weight[i]和 物品价值value[i]。剩下的过程类似。

    1. class Solution:
    2. def lastStoneWeightII(self, stones: List[int]) -> int:
    3. dp = [0] * 15001
    4. total_sum = sum(stones)
    5. target = total_sum // 2
    6. for stone in stones:
    7. for j in range(target, stone - 1, -1):
    8. dp[j] = max(dp[j], dp[j - stone] + stone)
    9. return total_sum - dp[target] - dp[target]

    第二题

    494. Target Sum

    You are given an integer array nums and an integer target.

    You want to build an expression out of nums by adding one of the symbols '+' and '-' before each integer in nums and then concatenate all the integers.

    • For example, if nums = [2, 1], you can add a '+' before 2 and a '-' before 1 and concatenate them to build the expression "+2-1".

    Return the number of different expressions that you can build, which evaluates to target.

    假设加法的总和为x,那么减法对应的总和就是sum - x。

    所以我们要求的是 x - (sum - x) = target

    x = (target + sum) / 2

    此时问题就转化为,装满容量为x的背包,有几种方法。

    这里的x,就是bagSize,也就是我们后面要求的背包容量。

    1. class Solution:
    2. def findTargetSumWays(self, nums: List[int], target: int) -> int:
    3. total_sum = sum(nums)
    4. if abs(target) > total_sum:
    5. return 0
    6. if (target + total_sum) % 2 == 1:
    7. return 0
    8. target_sum = (target + total_sum) // 2
    9. dp = [[0] * (target_sum + 1) for _ in range(len(nums) + 1)]
    10. dp[0][0] = 1
    11. for i in range(1, len(nums) + 1):
    12. for j in range(target_sum + 1):
    13. dp[i][j] = dp[i - 1][j]
    14. if j >= nums[i - 1]:
    15. dp[i][j] += dp[i - 1][j - nums[i - 1]]
    16. return dp[len(nums)][target_sum]

    第三题

    474. Ones and Zeroes

    You are given an array of binary strings strs and two integers m and n.

    Return the size of the largest subset of strs such that there are at most m 0's and n 1's in the subset.

    A set x is a subset of a set y if all elements of x are also elements of y.

    这道题乍一看比较复杂,就像一道脑筋急转弯。

    本题中strs 数组里的元素就是物品,每个物品都是一个!而m 和 n相当于是一个背包,两个维度的背包。

    1. class Solution:
    2. def findMaxForm(self, strs: List[str], m: int, n: int) -> int:
    3. dp = [[0] * (n + 1) for _ in range(m + 1)]
    4. for s in strs:
    5. zeroNum = s.count('0')
    6. oneNum = len(s) - zeroNum
    7. for i in range(m, zeroNum - 1, -1):
    8. for j in range(n, oneNum - 1, -1):
    9. dp[i][j] = max(dp[i][j], dp[i - zeroNum][j - oneNum] + 1)
    10. return dp[m][n]

  • 相关阅读:
    linux查看系统信息
    工业交换机在安全方面有哪些注意事项?
    HTML基础
    动画师如何选择全身动捕设备制作动画?
    使用PyQt5创建图片查看器应用程序
    Ubuntu 使用 nginx 搭建 https 文件服务器
    深入理解强化学习——马尔可夫决策过程:马尔可夫决策过程和马尔可夫过程/马尔可夫奖励过程的区别
    外汇天眼:多位支持加息放缓!美元走弱黄金上涨
    VMware中LINUX系统配置NAT ssh远程访问
    【微软技术栈】C#.NET 内置数值转换
  • 原文地址:https://blog.csdn.net/Hanzq1997/article/details/132837070