Last Stone Weight

Last Stone Weight: Problem Explanation and Solution

The Last Stone Weight problem is a simulation exercise that involves repeatedly smashing the two heaviest stones until no more than one stone remains.

The goal is to determine the weight of the last stone or return 0 if all stones are destroyed.

Kth largest elements

Problem: 

You are given an array of integers stones where stones[i] represents the weight of the ith stone.

We want to run a simulation on the stones as follows:

  • At each step we choose the two heaviest stones, with weight x and y and smash them togethers
  • If x == y, both stones are destroyed
  • If x < y, the stone of weight x is destroyed, and the stone of weight y has new weight y - x.

Continue the simulation until there is no more than one stone remaining.

Return the weight of the last remaining stone or return 0 if none remain.

  • Recommended Time & Space Complexity

You should aim for a solution as good or better than O(nlogn) time and O(n) space, where n is the size of the input array.

Problem Breakdown

Key Rules

  1. At each step, select the two heaviest stones:

    • If their weights are equal, both are destroyed.
    • If their weights are unequal, the heavier stone is reduced by the weight of the lighter one, and the lighter one is destroyed.
  2. Continue until only one stone remains (or none).

  3. Return the weight of the remaining stone or 0 if no stones are left.

Explanation:

  • We smash 6 and 4 and are left with a 2, so the array becomes [2,3,2,2].
  • We smash 3 and 2 and are left with a 1, so the array becomes [1,2,2].
  • We smash 2 and 2, so the array becomes [1].

Constraints:

  • 1 <= stones.length <= 20
  • 1 <= stones[i] <= 100

Approach

Key Idea: Max-Heap Simulation

To efficiently retrieve the two heaviest stones, we use a max-heap. However, Python’s heapq library provides a min-heap implementation by default, so we simulate a max-heap by storing negative values.

Algorithm

Steps

  1. Initialize the Heap: Convert all stone weights to negative and push them into a heap.
  2. Simulation Loop:
    • Pop the two largest elements (smallest in the min-heap due to negative values).
    • If the stones are not equal, compute the new weight and push it back into the heap.
  3. Result:
    • If the heap is empty, return 0.
    • Otherwise, return the absolute value of the remaining stone.

There are mainly Four approach to solve this problem – 

  1. Sorting 
  2. Binary Search 
  3. Heap
  4. Bucket Sort 

1. Sorting

  • Time complexity: O(n^2logn)
  • Space complexity: O(1) or O(n)depending on the sorting algorithm.

2. Binary Search

Time & Space Complexity
  • Time complexity: O(n^2)
  • Space complexity:  or O(n) depending on the sorting algorithm.

Where m is the number of calls made to add().

2. Binary Search

Time & Space Complexity
  • Time complexity: O(n^2)
  • Space complexity: or O(n) depending on the sorting algorithm.

3. Heap

Time & Space Complexity
  • Time complexity: O(nlog⁡n)
  • Space complexity: or O(n) depending on the sorting algorithm.

4. Bucket Sort

Time & Space Complexity
  • Time complexity:  O(n+w)
  • Space complexity: O(w)

More Articles