Manage Stacks of Bricks
MediumP3Coding
You have N stacks of bricks with heights heights[i], and you want the stacks to end up with the heights listed in targets. A final stack does not have to come from the stack with the same index; you only need the final collection of heights to match the multiset targets. Adding one brick to a stack costs x effort and removing one brick costs y effort.
Return the minimum total effort.
Example 1
- Input
- heights = [1,5], targets = [3,3], x = 2, y = 1
- Output
- 6
- Why
- Raise the stack of height 1 to 3 (2 bricks, 4 effort) and lower the stack of height 5 to 3 (2 bricks, 2 effort).
Example 2
- Input
- heights = [5,1], targets = [1,5], x = 10, y = 10
- Output
- 0
- Why
- The stacks already match as a collection.
Constraints
- 1 <= N <= 10^5
- 0 <= heights[i], targets[i] <= 10^4
- 1 <= x, y <= 10^4
Hints
Give it a real attempt first. Run shows which examples fail.
Saved automatically
Run checks the 3 visible tests, Submit all 9. ⌘Enter runs, ⌘⇧Enter submits.