Bird
Raised Fist0
Interview Prepdp-knapsackmediumGoogleAmazon

Last Stone Weight II

Choose your preparation mode4 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Steps
setup

Calculate total sum and half

Compute the total sum of stones (23) and calculate half (11) to limit the DP array size.

💡 Focusing on sums up to half the total is key because the problem reduces to partitioning stones into two groups with minimal difference.
Line:total = sum(stones) half = total // 2
💡 The problem transforms into finding a subset of stones with sum as close as possible to half the total.
📊
Last Stone Weight II - Watch the Algorithm Execute, Step by Step
Watching the DP array update step-by-step reveals how the problem reduces to a subset sum variant and how the algorithm efficiently finds the closest sum to half the total.
Step 1/10
·Active fillAnswer cell
Initializing dp array
i\w01234567891011
i=0True???????????
half
Initializing dp array
i\w01234567891011
i=0TrueFalseFalseFalseFalseFalseFalseFalseFalseFalseFalseFalse
dp[0]=True
Item 0 - wt:2 val:0
i\w01234567891011
i=0TrueFalseTrueFalseFalseFalseFalseFalseFalseFalseFalseFalse
dp[2] updated
Item 1 - wt:7 val:0
i\w01234567891011
i=0TrueFalseTrueFalseFalseFalseFalseTrueFalseTrueFalseFalse
dp[7] updated
Item 2 - wt:4 val:0
i\w01234567891011
i=0TrueFalseTrueFalseTrueFalseTrueTrueFalseTrueFalseTrue
dp[4] updated
Item 3 - wt:1 val:0
i\w01234567891011
i=0TrueTrueTrueFalseTrueTrueTrueTrueTrueTrueTrueTrue
dp[1] updated
Item 4 - wt:8 val:0
i\w01234567891011
i=0TrueTrueTrueFalseTrueTrueTrueTrueTrueTrueTrueTrue
dp[8] updated
Item 5 - wt:1 val:0
i\w01234567891011
i=0TrueTrueTrueTrueTrueTrueTrueTrueTrueTrueTrueTrue
dp[3] updated
Item 5 - wt:1 val:0
i\w01234567891011
i=0TrueTrueTrueTrueTrueTrueTrueTrueTrueTrueTrueTrue
answer cell
Item 5 - wt:1 val:0
i\w01234567891011
i=0TrueTrueTrueTrueTrueTrueTrueTrueTrueTrueTrueTrue
final answer

Key Takeaways

The problem reduces to finding a subset sum closest to half the total sum.

This insight is hard to see from code alone but becomes clear when watching achievable sums build up.

Backward iteration in DP prevents reuse of the same stone multiple times in one iteration.

Visualizing the backward updates clarifies why the order of iteration matters.

The final answer is found by scanning the DP array from half downwards to find the best achievable sum.

Seeing the final scan step highlights how the minimal difference is computed from the DP results.

Practice

(1/5)
1. Given the following code, what is the output when coins = [1, 2, 5] and amount = 5?
def count_ways_space_opt(coins, amount):
    dp = [0] * (amount + 1)
    dp[0] = 1
    for coin in coins:
        for w in range(coin, amount + 1):
            dp[w] += dp[w - coin]
    return dp[amount]

coins = [1, 2, 5]
amount = 5
print(count_ways_space_opt(coins, amount))
easy
A. 4
B. 7
C. 5
D. 6

Solution

  1. Step 1: Trace dp array updates for each coin

    Start dp = [1,0,0,0,0,0]. After coin=1, dp = [1,1,1,1,1,1]. After coin=2, dp = [1,1,2,2,3,3]. After coin=5, dp = [1,1,2,2,3,6].
  2. Step 2: Confirm dp[5] value

    dp[5] = 6, representing 6 ways to make amount 5 with coins [1,2,5].
  3. Final Answer:

    Option D -> Option D
  4. Quick Check:

    Manual dp tracing matches output 6 [OK]
Hint: DP accumulates counts incrementally per coin [OK]
Common Mistakes:
  • Off-by-one in dp indexing
  • Miscounting after last coin iteration
  • Confusing permutations with combinations
2. You are given an array of positive integers and a number k. The task is to determine if the array can be partitioned into k subsets such that the sum of elements in each subset is equal. Which algorithmic approach guarantees an optimal solution for this problem?
easy
A. Greedy algorithm that repeatedly picks the largest element and tries to fit it into subsets
B. Dynamic programming with bitmask tabulation that tracks subset sums for all combinations
C. Simple backtracking without memoization that tries all possible assignments
D. Sorting the array and using a two-pointer technique to pair elements

Solution

  1. Step 1: Understand problem constraints

    The problem requires partitioning into equal sum subsets, which is a classic NP-complete problem requiring exploration of subsets.
  2. Step 2: Identify algorithmic pattern

    Dynamic programming with bitmask tabulation efficiently explores all subsets and tracks partial sums modulo target, guaranteeing optimality.
  3. Final Answer:

    Option B -> Option B
  4. Quick Check:

    Bitmask DP covers all subsets systematically [OK]
Hint: Bitmask DP tracks all subset sums efficiently [OK]
Common Mistakes:
  • Assuming greedy always works for equal sum partition
  • Using backtracking without memoization leads to timeouts
3. You need to find the minimum number of perfect square numbers that sum to a given integer n. Which algorithmic approach guarantees an optimal solution for this problem?
easy
A. Dynamic programming using unbounded knapsack pattern with bottom-up tabulation
B. Greedy approach picking the largest square first until the sum is reached
C. Simple recursion without memoization exploring all combinations
D. Sorting all squares and using binary search to find the closest sum

Solution

  1. Step 1: Understand problem constraints

    The problem requires the minimum count of perfect squares summing to n, which is a classic unbounded knapsack variant where items (squares) can be reused.
  2. Step 2: Identify algorithm that guarantees optimality

    Greedy fails because picking largest squares first can be suboptimal (e.g., n=12). Simple recursion is correct but inefficient. Binary search does not solve the combinatorial optimization. Bottom-up DP with unbounded knapsack pattern systematically explores all combinations and ensures optimality.
  3. Final Answer:

    Option A -> Option A
  4. Quick Check:

    DP approach always finds minimum count [OK]
Hint: Greedy fails; DP unbounded knapsack guarantees optimal [OK]
Common Mistakes:
  • Assuming greedy works for minimum perfect squares
  • Confusing top-down recursion with guaranteed efficiency
4. Consider the following code for computing the minimum number of perfect squares summing to n. What is the value of dp[4] after the outer loop iteration i = 4 completes?
easy
A. 4
B. 1
C. 3
D. 2

Solution

  1. Step 1: Trace dp[4] updates

    For i=4, j iterates over 1 and 2 because 1*1=1 <=4 and 2*2=4 <=4. - For j=1: dp[4] = min(inf, 1 + dp[3]) = 1 + dp[3] - For j=2: dp[4] = min(previous, 1 + dp[0]) = min(previous, 1 + 0) = 1 Since dp[0] = 0, dp[4] becomes 1.
  2. Step 2: Confirm dp[4] final value

    dp[4] = 1 means 4 can be represented as one perfect square (2*2). This matches the expected minimal count.
  3. Final Answer:

    Option B -> Option B
  4. Quick Check:

    dp[4] = 1 for 4 = 2^2 [OK]
Hint: dp[4] = 1 because 4 is a perfect square itself [OK]
Common Mistakes:
  • Off-by-one errors in loop
  • Ignoring dp[0] base case
5. What is the time complexity of the optimal job scheduling algorithm that sorts jobs by end time and uses binary search with a DP array of size n?
medium
A. O(n log W) where W is the maximum end time
B. O(n^2) due to nested loops over all jobs
C. O(n) because each job is processed once
D. O(n log n) due to sorting and binary searches for each job

Solution

  1. Step 1: Identify sorting cost

    Sorting n jobs by end time costs O(n log n).
  2. Step 2: Analyze DP loop with binary search

    For each job, binary search among ends array costs O(log n), repeated n times -> O(n log n).
  3. Final Answer:

    Option D -> Option D
  4. Quick Check:

    Sorting + n binary searches dominate; no nested O(n^2) loops [OK]
Hint: Sorting + binary search per job -> O(n log n) [OK]
Common Mistakes:
  • Assuming O(n^2) due to DP
  • Ignoring binary search cost
  • Confusing max end time W with input size n