LeetCode Python/Java/C++/JS > Dynamic Programming > 1049. Last Stone Weight II > Solved in Python, C#, C++, Java, JavaScript, Go, Ruby > GitHub or Repost
LeetCode link: 1049. Last Stone Weight II, difficulty: Medium.
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 weightxis destroyed, and the stone of weightyhas new weighty - 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
Hint 1
Think of the final answer as a sum of weights with + or - sign symbols in front of each weight. Actually, all sums with 1 of each sign symbol are possible.
Hint 2
Use dynamic programming: for every possible sum with N stones, those sums +x or -x is possible with N+1 stones, where x is the value of the newest stone. (This overcounts sums that are all positive or all negative, but those don't matter.)
Intuition
- This problem can be solved by brute force, that is, find all subsets of the array, see if the sum of each subset array is close to half of the sum of the complete array, and find the one that is closest. But when we see
stones.length <= 30, we know that such a solution will definitely time out. - So we need to change our thinking. The question is equivalent to finding the minimum difference between the sums of the two arrays after splitting. If we find a subset array whose sum is closest to half of the sum of the complete array, then it is the subset array we want.
- Then this problem will become a
0/1 Knapsack Problem.
Pattern of "Dynamic Programming"
"Dynamic Programming" requires the use of the dp array to store the results. The value of dp[i][j] can be converted from its previous (or multiple) values through a formula. Therefore, the value of dp[i][j] is derived step by step, and it is related to the previous dp record value.
"Dynamic programming" is divided into five steps
- Determine the meaning of each value of the array
dp. - Initialize the value of the array
dp. - Fill in the
dpgrid data in order according to an example. - Based on the
dpgrid data, derive the recursive formula. - Write a program and print the
dparray. If it is not as expected, adjust it.
Detailed description of these five steps
- Determine the meaning of each value of the array
dp.- First determine whether
dpis a one-dimensional array or a two-dimensional array. Aone-dimensional rolling arraymeans that the values of the array are overwritten at each iteration. Most of the time, usingone-dimensional rolling arrayinstead oftwo-dimensional arraycan simplify the code; but for some problems, such as operating "two swappable arrays", for the sake of ease of understanding, it is better to usetwo-dimensional array. - Try to use the meaning of the
return valuerequired by the problem as the meaning ofdp[i](one-dimensional) ordp[i][j](two-dimensional). It works about 60% of the time. If it doesn't work, try other meanings. - Try to save more information in the design. Repeated information only needs to be saved once in a
dp[i]. - Use simplified meanings. If the problem can be solved with
boolean value, don't usenumeric value.
- First determine whether
- Initialize the value of the array
dp. The value ofdpinvolves two levels:- The length of
dp. Usually:condition array length plus 1orcondition array length. - The value of
dp[i]ordp[i][j].dp[0]ordp[0][0]sometimes requires special treatment.
- The length of
- Fill in the
dpgrid data in order according to an example.- The "recursive formula" is the core of the "dynamic programming" algorithm. But the "recursive formula" is obscure. If you want to get it, you need to make a table and use data to inspire yourself.
- If the original example is not good enough, you need to redesign one yourself.
- According to the example, fill in the
dpgrid data "in order", which is very important because it determines the traversal order of the code. - Most of the time, from left to right, from top to bottom. But sometimes it is necessary to traverse from right to left, from bottom to top, from the middle to the right (or left), such as the "palindrome" problems. Sometimes, it is necessary to traverse a line twice, first forward and then backward.
- When the order is determined correctly, the starting point is determined. Starting from the starting point, fill in the
dpgrid data "in order". This order is also the order in which the program processes. - In this process, you will get inspiration to write a "recursive formula". If you can already derive the formula, you do not need to complete the grid.
- Based on the
dpgrid data, derive the recursive formula.- There are three special positions to pay attention to:
dp[i - 1][j - 1],dp[i - 1][j]anddp[i][j - 1], the currentdp[i][j]often depends on them. - When operating "two swappable arrays", due to symmetry, we may need to use
dp[i - 1][j]anddp[i][j - 1]at the same time.
- There are three special positions to pay attention to:
- Write a program and print the
dparray. If it is not as expected, adjust it.- Focus on analyzing those values that are not as expected.
After reading the above, do you feel that "dynamic programming" is not that difficult? Try to solve this problem. 🤗
Pattern of "0/1 Knapsack Problem"
The typical "0/1 knapsack problem" means that each "item" can only be used once to fill the "knapsack". "Items" have "weight" and "value" attributes. Find the maximum value of "items" that can be stored in the "knapsack".
Its characteristics are: there is a set of numbers, each number can only be used once, and through some calculation, another number is obtained. The question can also be turned into whether it can be obtained? How many variations are there? And so on.
Because "0/1 Knapsack Problem" belongs to "Dynamic Programming", I will explain it in the pattern of "Dynamic Programming".
Determine what each value of the array
dprepresents.- Prefer one-dimensional rolling array because the code is concise.
- Determine what is "item" and what is "knapsack".
- If
dp[j]is a boolean value, thendp[j]indicates whether thesumof the firstiitems can getj. - If
dp[j]is a numerical value, thendp[j]indicates the maximum (or minimum) value thatdp[j]can reach using the firstiitems.
- If
Initialize the value of the array
dp.- Determine the size of the "knapsack". It is necessary to add 1 to the size of the knapsack, that is, insert
dp[0]as the starting point, which is convenient for understanding and reference. dp[0]sometimes needs special treatment.
- Determine the size of the "knapsack". It is necessary to add 1 to the size of the knapsack, that is, insert
According to an example, fill in the
dpgrid data "in order".- First in the outer loop, traverse the items.
- Then in the inner loop, traverse the knapsack size.
- When traversing the knapsack size, since
dp[j]depends ondp[j]anddp[j - weights[i]], we should traverse thedparray from right to left. - Please think about whether it is possible to traverse the
dparray fromleft to right?
According to the
dpgrid data, derive the "recursive formula".If
dp[j]is a boolean value:dp[j] = dp[j] || dp[j - items[i]]If
dp[j]is a numeric value:dp[j] = min_or_max(dp[j], dp[j - weights[i]] + values[i])
Write a program and print the
dparray. If it is not as expected, adjust it.
Step-by-Step Solution
- Determine the meaning of the
dp[j]dp[j]represents whether it is possible tosumthe firstistonesto getj.dp[j]is a boolean.
Determine the
dparray's initial valueUse an example:
stones = [2,7,4,1,8,1], so 'half of the sum' is 11. The `size` of the knapsack is `11 + 1`, and the `items` are `stones`. So after initialization, the 'dp' array would be: # 0 1 2 3 4 5 6 7 8 9 10 11 # T F F F F F F F F F F F # dp # 2 # 7 # 4 # 1 # 8 # 1dp[0]is set totrue, indicating that an empty knapsack can be achieved by not using anystones. In addition, it is used as the starting value, and the subsequentdp[j]will depend on it. If it isfalse, all values ofdp[j]will befalse.dp[j] = false (j != 0), indicating that it is impossible to getjwith nostones.
Fill in the
dpgrid data "in order" according to an example.1. Use the first stone '2'. # 0 1 2 3 4 5 6 7 8 9 10 11 # T F F F F F F F F F F F # 2 T F T F F F F F F F F F # dp2. Use the second stone '7'. # 0 1 2 3 4 5 6 7 8 9 10 11 # T F F F F F F F F F F F # 2 T F T F F F F F F F F F # 7 T F T F F F F T F T F F3. Use the third stone '4'. # 0 1 2 3 4 5 6 7 8 9 10 11 # T F F F F F F F F F F F # 2 T F T F F F F F F F F F # 7 T F T F F F F F T F F F # 4 T F T F T F T T F T F T # dp # ...Based on the
dpgrid data, derive the "recursive formula".dp[j] = dp[j] || dp[j - stones[i]]Write a program and print the
dparray. If it is not as expected, adjust it.
Complexity
Time complexity
O(n * sum/2)
Space complexity
O(sum/2)
Python #
class Solution:
def lastStoneWeightII(self, stones: List[int]) -> int:
sum_ = sum(stones)
dp = [False] * (sum_ // 2 + 1)
dp[0] = True
for stone in stones:
# If not traversing in reverse order, the newly assigned value `dp[j]` will act as `dp[j - stone]` later,
# then the subsequent `dp[j]` will be affected. But each `stone` can only be used once!
for j in range(len(dp) - 1, 0, -1):
if j < stone:
break
dp[j] = dp[j] or dp[j - stone]
for i in range(len(dp) - 1, -1, -1):
if dp[i]:
return sum_ - i * 2
C# #
public class Solution {
public int LastStoneWeightII(int[] stones) {
var sum = stones.Sum();
var dp = new bool[sum / 2 + 1];
dp[0] = true;
foreach (int stone in stones) {
for (var j = dp.GetUpperBound(0); j >= stone; j--) {
dp[j] = dp[j] || dp[j - stone];
}
}
for (var j = dp.GetUpperBound(0); j >= 0; j--) {
if (dp[j]) {
return sum - j * 2;
}
}
throw new ArithmeticException("lastStoneWeightII() has a logical error!");
}
}
C++ #
class Solution {
public:
int lastStoneWeightII(vector<int>& stones) {
auto sum = reduce(stones.begin(), stones.end());
auto dp = vector<bool>(sum / 2 + 1);
dp[0] = true;
for (auto stone : stones) {
for (auto j = dp.size() - 1; j >= stone; j--) {
dp[j] = dp[j] || dp[j - stone];
}
}
for (auto i = dp.size() - 1; i >= 0; i--) {
if (dp[i]) {
return sum - i * 2;
}
}
throw logic_error("lastStoneWeightII() has a logical error!");
}
};
Java #
class Solution {
public int lastStoneWeightII(int[] stones) {
var sum = IntStream.of(stones).sum();
var dp = new boolean[sum / 2 + 1];
dp[0] = true;
for (var stone : stones) {
for (var j = dp.length - 1; j >= stone; j--) {
dp[j] = dp[j] || dp[j - stone];
}
}
for (var j = dp.length - 1; j >= 0; j--) {
if (dp[j]) {
return sum - j * 2;
}
}
throw new ArithmeticException("lastStoneWeightII() has a logical error!");
}
}
JavaScript #
var lastStoneWeightII = function (stones) {
const sum = _.sum(stones)
const dp = Array(Math.floor(sum / 2) + 1).fill(false)
dp[0] = true
for (const stone of stones) {
for (let j = dp.length - 1; j >= stone; j--) {
dp[j] = dp[j] || dp[j - stone]
}
}
for (let j = dp.length - 1; j >= 0; j--) {
if (dp[j]) {
return sum - j * 2
}
}
};
Go #
func lastStoneWeightII(stones []int) int {
sum := 0
for _, stone := range stones {
sum += stone
}
dp := make([]bool, sum / 2 + 1)
dp[0] = true
for _, stone := range stones {
for j := len(dp) - 1; j >= stone; j-- {
dp[j] = dp[j] || dp[j - stone]
}
}
for j := len(dp) - 1; j >= 0; j-- {
if dp[j] {
return sum - j * 2
}
}
return -1 // This line should be unreachable. It represents function has a logical error.
}
Ruby #
def last_stone_weight_ii(stones)
sum = stones.sum
dp = Array.new(sum / 2 + 1, false)
dp[0] = true
stones.each do |stone|
(1...dp.size).reverse_each do |j|
break if j < stone
dp[j] = dp[j] || dp[j - stone]
end
end
(0...dp.size).reverse_each do |j|
return sum - j * 2 if dp[j]
end
end
Other languages
Welcome to contribute code to LeetCode.blog GitHub -> 1049. Last Stone Weight II. Thanks!Intuition of solution 2: Traverse "dp" from Left to Right (Recommended)
In solution 1, the traversal order is from right to left which really matters.
During the interview, you need to remember it. Is there any way to not worry about the traversal order?
As long as you copy the original Click to view the answer
dp and reference the value of the copy, you don't have to worry about the original dp value being modified.
Complexity
Time complexity
O(n * sum/2)
Space complexity
O(n * sum/2)
Python #
class Solution:
def lastStoneWeightII(self, stones: List[int]) -> int:
sum_ = sum(stones)
dp = [False] * (sum_ // 2 + 1)
dp[0] = True
for stone in stones:
dc = dp.copy()
for j in range(stone, len(dp)):
dp[j] = dc[j] or dc[j - stone]
for i in range(len(dp) - 1, -1, -1):
if dp[i]:
return sum_ - i * 2
C# #
public class Solution
{
public int LastStoneWeightII(int[] stones)
{
int sum = stones.Sum();
var dp = new bool[sum / 2 + 1];
dp[0] = true;
foreach (int stone in stones)
{
var dc = (bool[]) dp.Clone();
for (var j = stone; j < dp.Length; j++)
{
dp[j] = dc[j] || dc[j - stone];
}
}
for (var j = dp.GetUpperBound(0); j >= 0; j--)
{
if (dp[j])
{
return sum - j * 2;
}
}
throw new ArithmeticException("lastStoneWeightII() has a logical error!");
}
}
C++ #
class Solution {
public:
int lastStoneWeightII(vector<int>& stones) {
auto sum = reduce(stones.begin(), stones.end());
auto dp = vector<bool>(sum / 2 + 1);
dp[0] = true;
for (auto stone : stones) {
auto dc = dp;
for (auto j = stone; j < dp.size(); j++) {
dp[j] = dc[j] || dc[j - stone];
}
}
for (auto i = dp.size() - 1; i >= 0; i--) {
if (dp[i]) {
return sum - i * 2;
}
}
throw logic_error("lastStoneWeightII() has a logical error!");
}
};
Java #
class Solution {
public int lastStoneWeightII(int[] stones) {
var sum = IntStream.of(stones).sum();
var dp = new boolean[sum / 2 + 1];
dp[0] = true;
for (var stone : stones) {
var dc = dp.clone();
for (var j = stone; j < dp.length; j++) {
dp[j] = dc[j] || dc[j - stone];
}
}
for (var j = dp.length - 1; j >= 0; j--) {
if (dp[j]) {
return sum - j * 2;
}
}
throw new ArithmeticException("lastStoneWeightII() has a logical error!");
}
}
JavaScript #
var lastStoneWeightII = function (stones) {
const sum = _.sum(stones)
const dp = Array(Math.floor(sum / 2) + 1).fill(false)
dp[0] = true
for (const stone of stones) {
const dc = [...dp]
for (let j = stone; j < dp.length; j++) {
dp[j] = dc[j] || dc[j - stone]
}
}
for (let j = dp.length - 1; j >= 0; j--) {
if (dp[j]) {
return sum - j * 2
}
}
};
Go #
func lastStoneWeightII(stones []int) int {
sum := 0
for _, stone := range stones {
sum += stone
}
dp := make([]bool, sum / 2 + 1)
dp[0] = true
for _, stone := range stones {
dc := slices.Clone(dp)
for j := stone; j < len(dp); j++ {
dp[j] = dc[j] || dc[j - stone]
}
}
for j := len(dp) - 1; j >= 0; j-- {
if dp[j] {
return sum - j * 2
}
}
return -1 // This line should be unreachable. It represents function has a logical error.
}
Ruby #
def last_stone_weight_ii(stones)
sum = stones.sum
dp = Array.new(sum / 2 + 1, false)
dp[0] = true
stones.each do |stone|
dc = dp.clone
(stone...dp.size).each do |j|
dp[j] = dc[j] || dc[j - stone]
end
end
(0...dp.size).reverse_each do |j|
return sum - j * 2 if dp[j]
end
end