动态规划-Last Stone Weight II

2020-01-11 17:47:59

问题描述:

问题求解:

本题和另一题target sum非常类似。target sum的要求是在一个数组中随机添加正负号,使得最终得到的结果是target,这个题目被证明和背包问题是同一个问题,只是需要进行一下转化。

本题其实也是一个套壳题目,只是这次的壳套的更加隐蔽,对于本题来说,其实核心就是将其划分成两个堆,我们需要的是两堆的diff最小。

那么就需要另一个技巧了,我们不会直接使用dp去求这个最小值,而是使用dp去判断对于小堆中的和的可能性,最后在遍历检索一遍得到最小的diff即可。

    public int lastStoneWeightII(int[] stones) {
        int n = stones.length;
        int sum = 0;
        for (int num : stones) sum += num;
        int[] dp= new int[sum / 2 + 1];
        dp[0] = 1;
        for (int i = 0; i < n; i++) {
            for (int w = sum / 2; w >= 0; w--) {
                if (stones[i] <= w) dp[w] = dp[w] + dp[w - stones[i]];
            }
        }
        int res = Integer.MAX_VALUE;
        for (int i = 0; i < dp.length; i++) {
            if (dp[i] > 0) res = Math.min(res, sum - 2 * i);
        }
        return res;
    }

  

原文地址:https://www.cnblogs.com/hyserendipity/p/12180610.html

时间: 2024-08-30 17:57:57

动态规划-Last Stone Weight II的相关文章

LeetCode 1049. Last Stone Weight II

原题链接在这里:https://leetcode.com/problems/last-stone-weight-ii/ 题目: We have a collection of rocks, each rock has a positive integer weight. Each turn, we choose any two rocks and smash them together.  Suppose the stones have weights x and y with x <= y. 

LeetCode 1046. Last Stone Weight

原题链接在这里:https://leetcode.com/problems/last-stone-weight/ 题目: We have a collection of rocks, each rock has a positive integer weight. Each turn, we choose the two heaviest rocks and smash them together.  Suppose the stones have weights x and y with x

【动态规划】简单背包问题II

问题 B: [动态规划]简单背包问题II 时间限制: 1 Sec  内存限制: 64 MB提交: 21  解决: 14[提交][状态][讨论版] 题目描述 张琪曼:“为什么背包一定要完全装满呢?尽可能多装不就行了吗?” 李旭琳:“你说得对,这和墨老师曾告诉我们的‘日中则昃,月满则亏’是一个道理.”所以,现在的问题是,她们有一个背包容量为v(正整数,0≤v≤20000),同时有n个魔法石(0≤n≤30),每个魔法石有一个体积 (正整数).要求从n个魔法石中,任取若干个装入包内,使背包的剩余空间为最

hdu 4388 Stone Game II

Stone Game II HDU - 4388 题目大意: 给出n堆物品,每堆物品都有若干件,现在A和B进行游戏,每人每轮操作一次,按照如下规则: 1. 任意选择一个堆,假设该堆有x个物品,从中选择k个,要保证0<k<x且0<(x^k)<k. 2. 再增加一个大小为x^k的堆(也就相当于将一个x个物品的堆变成一个k个物品的堆和一个x^k个物品的堆),另外有一个技能,可以将这个大小为x^k的堆变成(2*k)^x的堆,但是这个技能每个人只有一次机会可以使用. 现在问两人轮流操作,都采

HDU 4388 Stone Game II {博弈||找规律}

Stone Game II Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 531    Accepted Submission(s): 300 Problem Description Stone Game II comes. It needs two players to play this game. There are some p

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

题目标签:Greedy 利用priority queue, 把石头重量都存入 pq, 每次取最大两个比较,存入差值,直到pq 只剩最后一个. Java Solution: Runtime:  1 ms, faster than 92.5% Memory Usage: 37.1 MB, less than 100% 完成日期:02/14/2020 关键点:priority queue class Solution { public int lastStoneWeight(int[] stones)

Stone Game II

Description There is a stone game.At the beginning of the game the player picks n piles of stones in a circle. The goal is to merge the stones in one pile observing the following rules: At each step of the game,the player can merge two adjacent piles

1046. Last Stone Weight

We have a collection of rocks, each rock has a positive integer weight. Each turn, we choose the two heaviest rocks 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 ar

[题解+总结]动态规划大合集II

1.前言 大合集总共14道题,出自江哥之手(这就没什么好戏了),做得让人花枝乱颤.虽说大部分是NOIP难度,也有简单的几道题目,但是还是做的很辛苦,有几道题几乎没思路,下面一道道边看边分析一下. 2.lis 最长上升子序列 唯一一道裸题,但是O(n^2)过不了,临时看了看O(n log n)的二分做法和线段树做法.先来讲讲简单的二分做法,其本质就是在O(n^2)上进行优化,需要证明一个结论.设当前处理数列第k位,存在: (1)a[i]<a[j]<a[k]: (2)i<j<k: (3