1049. Last Stone Weight II LeetCode Solution

In this guide, you will get 1049. Last Stone Weight II LeetCode Solution with the best time and space complexity. The solution to Last Stone Weight II problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.

Table of Contents

  1. Problem Statement
  2. Complexity Analysis
  3. Last Stone Weight II solution in C++
  4. Last Stone Weight II solution in Java
  5. Last Stone Weight II solution in Python
  6. Additional Resources
1049. Last Stone Weight II LeetCode Solution image

Problem Statement of Last Stone Weight II

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.

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

Complexity Analysis

  • Time Complexity:
  • Space Complexity:

1049. Last Stone Weight II LeetCode Solution in C++

class Solution {
 public:
  int lastStoneWeightII(vector<int>& stones) {
    const int sum = accumulate(stones.begin(), stones.end(), 0);
    vector<bool> dp(sum + 1);
    dp[0] = true;
    int s = 0;

    for (int stone : stones)
      for (int w = sum / 2; w > 0; --w) {
        if (w >= stone)
          dp[w] = dp[w] || dp[w - stone];
        if (dp[w])
          s = max(s, w);
      }

    return sum - 2 * s;
  }
};
/* code provided by PROGIEZ */

1049. Last Stone Weight II LeetCode Solution in Java

class Solution {
  public int lastStoneWeightII(int[] stones) {
    final int sum = Arrays.stream(stones).sum();
    boolean[] dp = new boolean[sum + 1];
    dp[0] = true;
    int s = 0;

    for (int stone : stones)
      for (int w = sum / 2; w > 0; --w) {
        if (w >= stone)
          dp[w] = dp[w] || dp[w - stone];
        if (dp[w])
          s = Math.max(s, w);
      }

    return sum - 2 * s;
  }
}
// code provided by PROGIEZ

1049. Last Stone Weight II LeetCode Solution in Python

class Solution:
  def lastStoneWeightII(self, stones: list[int]) -> int:
    summ = sum(stones)
    s = 0
    dp = [True] + [False] * summ

    for stone in stones:
      for w in range(summ // 2 + 1)[::-1]:
        if w >= stone:
          dp[w] = dp[w] or dp[w - stone]
        if dp[w]:
          s = max(s, w)

    return summ - 2 * s
# code by PROGIEZ

Additional Resources

Happy Coding! Keep following PROGIEZ for more updates and solutions.