Last Stone Weight II

欢迎关注H寻梦人公众号

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.

Example1:

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.

Example2:

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

Example3:

Input: stones = [1,2]
Output: 1

题目中文释义:

题解:

从上面题目的意思,其实我们可以转换一种角度来看待这个问题:

这个题目实质上其实就是,将所有的石子分为两堆,求两堆石子的最小绝对值的问题,最理想的答案是两堆石子一样多,但实际上可能会有偏差;假定所有的石子和为sum, 那么最终的一堆石子中必定有一堆的石子总和<=sum/2的,并且我们让其最大化。

因此,这个问题就可以转为背包为sum/2的01背包问题, 假定其值为dp[cap]

那么,最终的结果为 sum - 2*dp[cap]

证明:两堆石子的重量分别为x,y

结果为:|x-y|

假设x为其中较小的,那么结果为: y-x = (x+y)-2*x

因此,这问题就出来了,如下:


public int lastStoneWeightII(int[] stones) {
int sum = 0;
for (int stone : stones) {
sum += stone;
} int cap = sum / 2;
int[] dp = new int[cap +1];
for (int stone : stones) {
for (int i = cap; i >= stone; i--) {
dp[i] = Math.max(dp[i],dp[i-stone]+stone);
}
} return sum - 2*dp[cap]; }

Leetcode--Last Stone Weight II的更多相关文章

  1. LeetCode 1049. Last Stone Weight II

    原题链接在这里:https://leetcode.com/problems/last-stone-weight-ii/ 题目: We have a collection of rocks, each ...

  2. leetcode 1049 Last Stone Weight II(最后一块石头的重量 II)

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

  3. LeetCode 1140. Stone Game II

    原题链接在这里:https://leetcode.com/problems/stone-game-ii/ 题目: Alex and Lee continue their games with pile ...

  4. 动态规划-Last Stone Weight II

    2020-01-11 17:47:59 问题描述: 问题求解: 本题和另一题target sum非常类似.target sum的要求是在一个数组中随机添加正负号,使得最终得到的结果是target,这个 ...

  5. LeetCode 1046. Last Stone Weight

    原题链接在这里:https://leetcode.com/problems/last-stone-weight/ 题目: We have a collection of rocks, each roc ...

  6. leetcode_1049. Last Stone Weight II_[DP]

    1049. Last Stone Weight II https://leetcode.com/problems/last-stone-weight-ii/ 题意:从一堆石头里任选两个石头s1,s2, ...

  7. LeetCode 1046. 最后一块石头的重量(1046. Last Stone Weight) 50

    1046. 最后一块石头的重量 1046. Last Stone Weight 题目描述 每日一算法2019/6/22Day 50LeetCode1046. Last Stone Weight Jav ...

  8. leetcode 57 Insert Interval & leetcode 1046 Last Stone Weight & leetcode 1047 Remove All Adjacent Duplicates in String & leetcode 56 Merge Interval

    lc57 Insert Interval 仔细分析题目,发现我们只需要处理那些与插入interval重叠的interval即可,换句话说,那些end早于插入start以及start晚于插入end的in ...

  9. LeetCode 877. Stone Game

    原题链接在这里:https://leetcode.com/problems/stone-game/ 题目: Alex and Lee play a game with piles of stones. ...

随机推荐

  1. java实现二叉树的Node节点定义手撕8种遍历(一遍过)

    java实现二叉树的Node节点定义手撕8种遍历(一遍过) 用java的思想和程序从最基本的怎么将一个int型的数组变成Node树状结构说起,再到递归前序遍历,递归中序遍历,递归后序遍历,非递归前序遍 ...

  2. Java语言学习day04--7月1日

    ###09数据类型转换_自动转换     * A:   自动类型转换         * a:表示范围小的数据类型转换成范围大的数据类型,这种方式称为自动类型转换             自动类型转 ...

  3. MySQL基础之写表(创建表)

    我的博客 工具 市面上的SQL可视化工具不少,我一般常用的主要就是这两个. 当然,IDEA也是集成了数据库可视化功能的.除了这些,还有DBeaver.SQLyog等等. 我比较喜欢DataGrip,我 ...

  4. java中的stream是啥?

    函数式编程的执行是惰性的,按顺序真正执行的时候才会执行相应的代码.方法: 函数式编程是安全的,用的是monad架构 1 public class StreamTest { 2 3 public sta ...

  5. nginx反向代理隐藏端口号和项目名

    可利用nginx反向代理隐藏端口号和项目名,直接输入ip即可访问对应的tomcat项目,配置nginx安装目录的nginx/conf/nginx.conf文件,修改如下:(开了两个web项目:项目名为 ...

  6. RAID5加热备盘

    RAID 5加热备盘 RAID 10磁盘阵列中最多允许50%的硬盘设备发生故障,但是存在这样一种极端情况,即同一RAID 1磁盘阵列中的硬盘设备若全部损坏,也会导致数据丢失.换句话说,在RAID 10 ...

  7. SD卡之二:SD总线访问模式

    SD 卡是以命令.回应.数据流进行通讯. 1.命令:命令的长度是48位,命令以'0'开始,第2位为'1'表示主机发往SD卡的命令,最后以CRC和结束位'1'结尾. 2.回应:回应的长度是48位或者13 ...

  8. UI自动化滑动登录

    一.使用OpenCV图像识别函数 1 import time 2 import cv2 3 import requests 4 from selenium import webdriver 5 fro ...

  9. JAVA 基础(1)开发环境的搭建以及开发工具的选择

    ​  我们现在还是在学习阶段因此我们不用配置那么多的jdk,配置一个jdk8就够应付日常的学习了.前面的文章我尽量写详细一些照顾刚入坑的朋友.后文还有教大家怎么使用企业版的idea. 一.开发环境的搭 ...

  10. Python技法:实现简单的递归下降Parser

    1. 算术运算表达式求值 在上一篇博文<Python技法:用re模块实现简易tokenizer>中,我们介绍了用正则表达式来匹配对应的模式,以实现简单的分词器.然而,正则表达式不是万能的, ...