Dsa
Dynamic Programming
Easy
Min Cost Carpet Replacement
You are tasked with replacing the carpets in a series of rooms in a hotel. Each room has a different cost per square foot to replace the carpet. Given a list of integers representing the conversion cost per square foot for each room, determine the minimum cost to replace carpets in all rooms, allowing for the possibility of replacing carpets in groups of three consecutive rooms at a discounted rate (only the room with the highest cost in that group counts towards the total cost). If there are fewer than three rooms left at the end, simply sum their costs.
Input:
- The first line contains an integer n (1 ≤ n ≤ 1000), the number of rooms.
- The second line contains n space-separated integers, where the i-th integer represents the cost of replacing carpet in room i.
Output:
- A single integer, the minimum total cost to replace carpets in all rooms.
Example:
Input:
5
10 20 30 5 15
Output:
40
Explanation:
For the rooms 1, 2, and 3, the highest cost (30) is counted.
For rooms 4 and 5, the costs (5 + 15) are counted individually, giving a total of 30 + 10 = 40.
Key concepts
dynamic_programminggreedycost_optimization
Practise this out loud — free
Start a mock interview on THIS exact question — a voice AI interviewer opens with it, pushes back like a real onsite, then hands you an instant scorecard.
🎙 Practise this question now