【陪伴式刷题】Day 38|动态规划|1049. 最后一块石头的重量 II(Last Stone Weight II)

刷题顺序按照代码随想录建议

题目描述

英文版描述

You are given an array of integers stones where stones[i] is the weight of the i(th) 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.

Example 1:

Input: stones = 2,7,4,1,8,1 Output: 1 Explanation: We can combine 2 and 4 to get 2, so the array converts to 2,7,1,8,1 then, we can combine 7 and 8 to get 1, so the array converts to 2,1,1,1 then, we can combine 2 and 1 to get 1, so the array converts to 1,1,1 then, we can combine 1 and 1 to get 0, so the array converts to 1, then that's the optimal value.

Example 2:

Input: stones = 31,26,33,21,40 Output: 5

Constraints:

  • 1 <= stones.length <= 30
  • 1 <= stones[i] <= 100

英文版地址

leetcode.com/problems/la...

中文版描述

有一堆石头,用整数数组 stones 表示。其中 stones[i] 表示第 i 块石头的重量。

每一回合,从中选出任意两块石头 ,然后将它们一起粉碎。假设石头的重量分别为 xy,且 x <= y。那么粉碎的可能结果如下:

  • 如果 x == y,那么两块石头都会被完全粉碎;
  • 如果 x != y,那么重量为 x 的石头将会完全粉碎,而重量为 y 的石头新重量为 y-x

最后,最多只会剩下一块 石头。返回此石头 最小的可能重量 。如果没有石头剩下,就返回 0

示例 1:

输入: stones = 2,7,4,1,8,1 输出: 1 解释: 组合 2 和 4,得到 2,所以数组转化为 2,7,1,8,1, 组合 7 和 8,得到 1,所以数组转化为 2,1,1,1, 组合 2 和 1,得到 1,所以数组转化为 1,1,1, 组合 1 和 1,得到 0,所以数组转化为 1,这就是最优值。

示例 2:

输入: stones = 31,26,33,21,40 输出: 5

提示:

  • 1 <= stones.length <= 30
  • 1 <= stones[i] <= 100

中文版地址

leetcode.cn/problems/la...

解题方法

动态规划

java 复制代码
class Solution {
    public int lastStoneWeightII(int[] stones) {
        int sum = 0;
        for (int i = 0; i < stones.length; i++) {
            sum += stones[i];
        }
        int target = sum / 2;
        // 1. 确定dp数组含义:dp[row][col] 表示任选[0,row]中的石头(每个石头最多选一次)放入容积为col的背包,总价值为dp[row][col]
        int[][] dp = new int[stones.length][target + 1];
        // 2. 初始化dp数组(第一行)
        for (int i = 0; i < target + 1; i++) {
            if (i >= stones[0]) {
                dp[0][i] = stones[0];
            }
        }
        // 3. 确定遍历顺序
        for (int row = 1; row < stones.length; row++) {
            for (int col = 0; col < target + 1; col++) {
                // 4. 确定递推公式
                if (col < stones[row]) {
                    dp[row][col] = dp[row - 1][col];
                } else {
                    dp[row][col] = Math.max(dp[row - 1][col], dp[row - 1][col - stones[row]] + stones[row]);
                }
            }
        }
        return sum - dp[stones.length - 1][target] - dp[stones.length - 1][target];
    }
}

复杂度分析

  • 时间复杂度:O(n^2),其中n是数组长度
  • 空间复杂度:O(n*target),其中n是数组长度,target 是整个数组的元素和的一半
相关推荐
sylviiiiiia3 分钟前
leetcode hot 100
python·算法·leetcode
带刺的坐椅10 分钟前
不只是 Token 流:Solon AI 4.1 的语义化聊天事件
java·stream·event·solon-ai
延凡科技16 分钟前
建筑节能新视角:智慧冷站的数字化实践
java·人工智能·能源·暖通
www_aiyuanma_vip34 分钟前
Java个人博客系统
java·开发语言
秋名RG39 分钟前
Java 集合框架完全指南:从核心接口到 JDK 21 有序集合新特性
java·windows
蜡台1 小时前
Kotlin 五大作用域函数详解|let/run/apply/also/with 选型指南+实战避坑
android·java·kotlin
AC赳赳老秦1 小时前
农产品公开数据应用:OpenClaw 抓取农产品价格、产销公开数据,实现农产品行情动态监测
java·c语言·javascript·python·php·deepseek·openclaw
1001101_QIA1 小时前
从 CPU 到 GPU:高速缓存、指令并行与 CUDA 执行原理入门
java·后端·spring
API快乐传递者2 小时前
淘宝海外商品详情接口实战指南:从全球开放平台到跨境铺货的全链路方案
java·前端·数据库
wabs6662 小时前
关于二叉树【力扣199.二叉树的右视图的思考】
数据结构·c++·算法·leetcode·二叉树·层序遍历