Imagine you're a contractor managing a painting project with n walls to paint. You have two types of painters at your disposal:
- Paid Painter: A skilled professional who paints wall
iintime[i]units and chargescost[i]dollars - Free Painter: An apprentice who works for free and paints any wall in exactly
1unit of time, but can only work when the paid painter is busy
The key insight is that while the paid painter works on one wall, the free painter can simultaneously work on other walls. If the paid painter takes 3 units to paint a wall, the free painter can complete 3 different walls during that same period!
Goal: Find the minimum cost to paint all n walls using this optimal work scheduling.
Input: Two arrays cost[] and time[] where cost[i] and time[i] represent the cost and time for the paid painter to paint wall i.
Output: The minimum total cost to paint all walls.
Input & Output
Visualization
Time & Space Complexity
We have n walls and need to consider up to n remaining walls for each
DP table of size nĆn, can be optimized to O(n) with 1D array
Constraints
- 1 ⤠n ⤠500 where n = cost.length = time.length
- 1 ⤠cost[i] ⤠106
- 1 ⤠time[i] ⤠500
- The free painter can only work when the paid painter is busy