11. Equalize the Towers
β GFG solution to the Equalize the Towers problem: find minimum cost to make all towers equal height using weighted median optimization technique. π
π§© Problem Description
π Examples
Example 1
Input: heights[] = [1, 2, 3], cost[] = [10, 100, 1000]
Output: 120
Explanation: The heights can be equalized by either "Removing one block from 3 and adding one in 1"
or "Adding two blocks in 1 and adding one in 2". Since the cost of operation in tower 3 is 1000,
the first process would yield 1010 while the second one yields 120.Example 2
Input: heights[] = [7, 1, 5], cost[] = [1, 1, 1]
Output: 6
Explanation: The minimum cost to equalize the towers is 6, achieved by setting all towers to height 5.π Constraints
β
My Approach
Weighted Median Optimization
π Time and Auxiliary Space Complexity
π§βπ» Code (C++)
β Code (Java)
π Code (Python)
π§ Contribution and Support
πVisitor Count
Last updated