Painting the Walls - Problem

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 i in time[i] units and charges cost[i] dollars
  • Free Painter: An apprentice who works for free and paints any wall in exactly 1 unit 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

example_1.py — Basic Case
$ Input: cost = [1,2,3,2], time = [1,2,3,2]
› Output: 3
šŸ’” Note: Paint wall 1 and wall 3 with paid painter. Paid painter takes 2+2=4 time units and costs 2+2=4. During those 4 time units, free painter can paint walls 0 and 2 (2 walls total). Wait, let me recalculate: Actually, paint wall 2 with paid painter (cost=3, time=3), and free painter paints the other 3 walls in those 3 time units. Total cost = 3.
example_2.py — Minimum Case
$ Input: cost = [2,1], time = [1,2]
› Output: 2
šŸ’” Note: Paint wall 0 with paid painter (cost=2, time=1). During that 1 time unit, free painter can paint 1 wall. But we have 2 walls total, so we need the paid painter to paint wall 1 as well. Actually, better: paint wall 1 with paid painter (cost=1, time=2). Free painter can paint wall 0 during those 2 time units. Total cost = 1. Wait, that's not enough time for 1 wall. Let me recalculate: Paint wall 0 (cost=2, time=1) with paid painter. Free painter paints wall 1 in that 1 time unit. Total cost = 2.
example_3.py — Edge Case Single Wall
$ Input: cost = [5], time = [3]
› Output: 5
šŸ’” Note: Only one wall, so paid painter must paint it. Cost = 5. Free painter has nothing to do.

Visualization

Tap to expand
šŸŽØ Paint Shop StrategyPaidPainterFreePainterWall ACost: $3Time: 3 unitsWall BCost: $2Time: 1 unitWall CCost: $4Time: 2 unitsPays $3, works 3 unitsFree painter handles2 walls in 3 time unitsOptimal Strategy:Use paid painter on Wall A (high time = more free capacity)Free painter completes Walls B & C during those 3 unitsTotal Cost: $3
Understanding the Visualization
1
Evaluate Each Wall
For each wall, consider the trade-off: pay for specialist OR use free capacity
2
Track Free Capacity
When specialist works for T time units, apprentices can paint T walls for free
3
Dynamic Programming
Build up optimal solutions: dp[j] = min cost to paint exactly j walls
4
Final Decision
Choose wall assignments that minimize total cost while painting all walls
Key Takeaway
šŸŽÆ Key Insight: Maximize the free painter's productivity by strategically assigning high-time walls to the paid painter, creating more opportunities for free work!

Time & Space Complexity

Time Complexity
ā±ļø
O(n²)

We have n walls and need to consider up to n remaining walls for each

n
2n
⚠ Quadratic Growth
Space Complexity
O(n²)

DP table of size nƗn, can be optimized to O(n) with 1D array

n
2n
⚠ Quadratic Space

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
Asked in
Google 45 Amazon 38 Microsoft 29 Meta 22
67.9K Views
Medium Frequency
~25 min Avg. Time
2.8K Likes
Ln 1, Col 1
Smart Actions
šŸ’” Explanation
AI Ready
šŸ’” Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen