Skip to content

Practice Minimum Cost to Merge Stones

Read the task, work through the examples, and check your approach against the constraints. Use the original LeetCode task for coding and submissions.

Hard#1000
LeetCode Problem

Minimum Cost to Merge Stones

Problem Statement

There are n piles of stones arranged in a row. The ith pile has stones[i] stones. A move consists of merging exactly k consecutive piles into one pile, and the cost of this move is equal to the total number of stones in these k piles. Return the minimum cost to merge all piles of stones into one pile. If it is impossible, return -1.

Source: Minimum Cost to Merge Stones on LeetCode. Use the original task for its coding environment and submissions.

Examples

# Example 1

Input
stones = [3,2,4,1], k = 2
Output
20

# Example 2

Input
stones = [3,2,4,1], k = 3
Output
-1

# Example 3

Input
stones = [3,5,1,2,6], k = 3
Output
25

Constraints

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

Related LeetCode Problems

Practice more problems similar to Minimum Cost to Merge Stones.

Review your practice with PhantomCodeAI

After trying the problem yourself, use the app to discuss your approach, complexity, and edge cases.