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.  The result of this smash is:

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

At the end, there is at most 1 stone left.  Return the smallest possible weight of this stone (the weight is 0 if there are no stones left.)

Example 1:

Input: [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.

Note:

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

题解:

If we choose any two rocks, we could divide the rocks into 2 groups.

And calculate the minimum diff between 2 groups‘ total weight.

Since stones.length <= 30, stone weight <= 100, maximum total weight could be 3000.

Let dp[i] denotes whether or not smaller group weight could be i. smaller goupe total weight is limited to 1500. Thus dp size is 1501. It becomes knapsack problem.

dp[0] = true. We don‘t need to choose any stone, and we could get group weight as 0.

For each stone, if we choose it we track dp[i-w] in the previoius iteration. If we don‘t choose it, track dp[i] from last iteration. Thus it is iterating from big to small.

Time Complexity: O(n*sum). n = stones.length. sum is total weight of stones.

Space: O(sum).

AC Java:

 1 class Solution {
 2     public int lastStoneWeightII(int[] stones) {
 3         if(stones == null || stones.length == 0){
 4             return 0;
 5         }
 6
 7         int sum = 0;
 8         boolean [] dp = new boolean[1501];
 9         dp[0] = true;
10
11         for(int w : stones){
12             sum += w;
13
14             for(int i = Math.min(sum, 1501); i>=w; i--){
15                 dp[i] = dp[i] | dp[i-w];
16             }
17         }
18
19         for(int i = sum/2; i>=0; i--){
20             if(dp[i]){
21                 return sum-i-i;
22             }
23         }
24
25         return 0;
26     }
27 }

Last Stone Weight进阶.

原文地址:https://www.cnblogs.com/Dylan-Java-NYC/p/11785116.html

时间: 2024-11-09 03:27:43

LeetCode 1049. Last Stone Weight II的相关文章

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

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)

动态规划-Last Stone Weight II

2020-01-11 17:47:59 问题描述: 问题求解: 本题和另一题target sum非常类似.target sum的要求是在一个数组中随机添加正负号,使得最终得到的结果是target,这个题目被证明和背包问题是同一个问题,只是需要进行一下转化. 本题其实也是一个套壳题目,只是这次的壳套的更加隐蔽,对于本题来说,其实核心就是将其划分成两个堆,我们需要的是两堆的diff最小. 那么就需要另一个技巧了,我们不会直接使用dp去求这个最小值,而是使用dp去判断对于小堆中的和的可能性,最后在遍历

Java [Leetcode 119]Pascal&#39;s Triangle II

题目描述: Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3,Return [1,3,3,1]. 解题思路: 每次在上一个list前面插入1,然后后面的每两个间相加赋值给前一个数. 代码描述: public class Solution { public List<Integer> getRow(int rowIndex) { List<Integer> r

leetcode 【 Pascal&#39;s Triangle II 】python 实现

题目: Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3,Return [1,3,3,1]. Note:Could you optimize your algorithm to use only O(k) extra space? 代码:oj测试通过 Runtime: 48 ms 1 class Solution: 2 # @return a list of intege

[LeetCode] Unique Binary Search Trees II (难以忍受的递归)

Given n, generate all structurally unique BST's (binary search trees) that store values 1...n. For example, Given n = 3, your program should return all 5 unique BST's shown below. 1 3 3 2 1 \ / / / \ 3 2 1 1 3 2 / / \ 2 1 2 3 class Solution { private

leetcode 119 Pascal&#39;s Triangle II ----- java

Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3,Return [1,3,3,1]. Note:Could you optimize your algorithm to use only O(k) extra space? 上一道题的延伸版,就是直接求出第k行的数,要求用o(k)的空间复杂度. 也是直接相加就可以了. public class Solution { pub

leetCode 119. Pascal&#39;s Triangle II 数组

119. Pascal's Triangle II Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3,Return [1,3,3,1]. Note:Could you optimize your algorithm to use only O(k) extra space? 代码如下:(使用双数组处理,未优化版) class Solution { public:     

LeetCode#119 Pascal&#39;s Triangle II

Problem Definition: Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3, Return [1,3,3,1]. Note: Could you optimize your algorithm to use only O(k) extra space? Solution: 1 def getRow(rowIndex): 2 pt=[] 3 for rn in