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. redis无损数据迁移

    在dba眼中,redis仅仅是一个缓存,不适合作为存储来使用,不管是redis-sentinel集群还是cluster集群,在redis主节点发生意外宕机时没有机制来保证主从节点数据的一致性.但是,很 ...

  2. 阿里云服务器的购买、基本配置、(xshell)远程连接、搭建环境

    一.服务器的购买 1.购买时间点:搞活动的时候.利用学生身份购买 (1)活动:想白嫖一台服务器 双十一,可以在双十一左右,时间提前一点,百度或B站,搜阿里云服务器.腾讯服务器(618可能也有) 一般, ...

  3. XCTF练习题---MISC---Ditf

    XCTF练习题---MISC---Ditf flag:flag{Oz_4nd_Hir0_lov3_For3ver} 解题步骤: 1.观察题目,下载附件 2.这道题是安恒办的一场比赛题目,下载附件以后是 ...

  4. Bugku CTF练习题---加密---ok

    Bugku CTF练习题---加密---ok flag:flag{ok-ctf-1234-admin} 解题步骤: 1.观察题目,发现规律 2.发现所有内容都是ook写的, 直接上网搜索一下原因,发现 ...

  5. spring盒springMVC整合父子容器问题:整合Spring时Service层为什么不做全局包扫描详解

    整合Spring时Service层为什么不做全局包扫描详解 一.Spring和SpringMVC的父子容器关系 1.讲问题之前要先明白一个关系 一般来说,我们在整合Spring和SpringMVC这两 ...

  6. 修改本地仓库Maven,设置镜像

    修改本地仓库的位置: 先建立 文件路径: 进入文件夹目录: 就可以看到一个配置文件夹 就在里面修改本地仓库的路径,设置阿里云镜像 添加本地阿里云镜像: 如下图: 内容如下: <mirror> ...

  7. 你不知道的JS 中——yield

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  8. 如何基于 ZEGO SDK 实现 Android 通话质量监测

    功能简介 在进行视频通话过程中,用户有时候会出现网络不好的情况,比如在进行多人视频通话或者多人唱歌时,我们需要实时显示用户的网络质量. 示例源码 参考 下载示例源码 获取源码. 相关源码请查看 &qu ...

  9. 太极限了,JDK的这个BUG都能被我踩到

    hello,大家好呀,我是小楼. 之前遇到个文件监听变更的问题,刚好这周末有空研究了一番,整理出来分享给大家. 从一次故障说起 我们还是从故障说起,这样更加贴近实际,也能让大家更快速理解背景. 有一个 ...

  10. kvm 虚拟化技术 1.1 安装

    1.·VMware开启虚拟化设置 2.安装一些虚拟化的必备插件 yum install -y qemu-kvm qemu-kvm-tools libvirt virt-manager virt-ins ...