Problem
You have N
stones in a row, and would like to create from them a pyramid. This pyramid should be constructed such that the height of each stone increases by one until reaching the tallest stone, after which the heights decrease by one. In addition, the start and end stones of the pyramid should each be one stone high.
You can change the height of any stone by paying a cost of 1
unit to lower its height by 1
, as many times as necessary. Given this information, determine the lowest cost method to produce this pyramid.
Examples
Example 1
|
|
Example 2
|
|
Example 3
|
|
Example 4
|
|
Example 5
|
|
Solution
Method 1 - Dynamic Programming with Peak Selection
Intuition
The key insight is that we need to try every possible position as the peak of the pyramid. For each peak position, we can determine the exact target heights (forming a mountain with peak at that position and ends at height 1), then calculate the cost to achieve those heights.
Approach
- For each possible peak position
i
from0
ton-1
, calculate the target pyramid - The target heights form:
[1, 2, 3, ..., peak_height, ..., 3, 2, 1]
- The peak height at position
i
ismin(i+1, n-i)
to ensure ends are height 1 - For each position
j
, calculate target height based on distance from peak - Calculate cost as sum of max(0, current_height - target_height) for all positions
- Return minimum cost across all possible peak positions
Code
|
|
|
|
|
|
|
|
Complexity
- ⏰ Time complexity:
O(n²)
, where n is the number of stones. We try each position as peak (O(n)) and for each peak calculate cost for all positions (O(n)) - 🧺 Space complexity:
O(1)
, only using constant extra space for calculations
Method 2 - Dynamic Programming Optimization
Intuition
The key insight is that we can precompute the maximum possible pyramid height at each position by considering constraints from both left and right sides. The maximum height at any position is limited by: the stone’s current height, the position index (since pyramid starts from 1), and the neighboring positions.
Approach
- Left Pass: For each position, calculate maximum possible height considering left-to-right constraints
left[i] = min(stones[i], min(left[i-1] + 1, i + 1))
- This ensures pyramid can increase by 1 and doesn’t exceed stone height
- Right Pass: For each position, calculate maximum possible height considering right-to-left constraints
right[i] = min(stones[i], min(right[i+1] + 1, n - i))
- This ensures pyramid can decrease by 1 toward the end
- Combine: Take minimum of left and right constraints for actual maximum height at each position
- Find Peak: The position with maximum combined height becomes our optimal peak
- Calculate Cost: Sum the reduction costs for the optimal pyramid configuration
Code
|
|
|
|
|
|
|
|
Complexity
- ⏰ Time complexity:
O(n)
, where n is the number of stones. We make three linear passes through the array: left pass, right pass, and cost calculation - 🧺 Space complexity:
O(n)
, for storing the left, right, and max_height arrays