- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions

# Program to find minimum number of bricks required to make k towers of same height in Python

Suppose we have a list of tower heights, and a positive value k. We want to select k towers and make them all the same height by adding more bricks, but using as few bricks as possible. We have to find the minimum number of bricks are needed to pick k towers and make them the same height.

So, if the input is like heights = [4, 7, 31, 14, 40] k = 3, then the output will be 17, as we can select 5, 8, and 15 which requires 17 bricks to make the same height.

To solve this, we will follow these steps −

- sort the list heights
- ans := infinity
- s := 0
- for each index i and value x in heights, do
- s := s + x
- if i >= k, then
- s := s - heights[i - k]

- if i >= k - 1, then
- ans := minimum of ans and (x * k - s)

- return ans

Let us see the following implementation to get better understanding −

## Example

class Solution: def solve(self, heights, k): heights.sort() ans = float("inf") s = 0 for i, x in enumerate(heights): s += x if i >= k: s -= heights[i - k] if i >= k - 1: ans = min(ans, x * k - s) return ans ob = Solution() heights = [5, 8, 32, 15, 41] k = 3 print(ob.solve(heights, k))

## Input

[5, 8, 32, 15, 41], 3

## Output

17

- Related Articles
- Program to find minimum number of operations required to make one number to another in Python
- Program to find minimum number of operations required to make lists strictly Increasing in python
- Program to find minimum number of operations required to make one string substring of other in Python
- Program to count number of minimum swaps required to make it palindrome in Python
- Program to find minimum number of groups in communication towers in C++?\n
- Program to find minimum number of deletions required from two ends to make list balanced in Python
- Program to count minimum number of operations required to make numbers non coprime in Python?
- Program to find minimum swaps required to make given anagram in python
- Program to find minimum remove required to make valid parentheses in Python
- Find the minimum number of preprocess moves required to make two strings equal in Python
- Program to find minimum number of hops required to reach end position in Python
- Program to find minimum number of flips required to have alternating values in Python
- Program to find minimum number of buses required to reach final target in python
- Program to find minimum time required to complete tasks with k time gap between same type tasks in Python
- Program to find minimum number of operations to make string sorted in Python

Advertisements