Painting the Walls - Problem

You are given two 0-indexed integer arrays, cost and time, of size n representing the costs and the time taken to paint n different walls respectively.

There are two painters available:

  • A paid painter that paints the ith wall in time[i] units of time and takes cost[i] units of money.
  • A free painter that paints any wall in 1 unit of time at a cost of 0. But the free painter can only be used if the paid painter is already occupied.

Return the minimum amount of money required to paint the n walls.

Input & Output

Example 1 — Basic Case
$ Input: cost = [1,2,3,2], time = [1,2,3,2]
Output: 3
💡 Note: Paint wall 0 with paid painter (cost 1, takes 1 time), free painter paints wall 1 during that time. Paint wall 2 with paid painter (cost 3, takes 3 time), free painter paints wall 3 during that time. Total cost = 1 + 3 = 4. But optimal is: paint walls 0,3 with paid painter (cost 1+2=3), free painter paints walls 1,2. Total = 3.
Example 2 — All Paid
$ Input: cost = [2,1,2,1], time = [1,2,1,1]
Output: 4
💡 Note: Paint all walls with paid painter costs 2+1+2+1=6. Better: paint walls 1,3 with paid painter (cost 1+1=2, time 2+1=3), free painter paints walls 0,2 during that time. Total cost = 2. Wait, let me recalculate... Actually optimal is paint walls 0,1 with paid (cost 2+1=3, time 1+2=3), free paints walls 2,3. But that doesn't work timing-wise. Correct answer is 4.
Example 3 — High Time Values
$ Input: cost = [7,3,8,2], time = [1,1,1,1]
Output: 5
💡 Note: Paint walls 1,3 with paid painter (cost 3+2=5, time 1+1=2), free painter paints walls 0,2 during that time. Total cost = 5.

Constraints

  • 1 ≤ cost.length ≤ 500
  • cost.length == time.length
  • 1 ≤ cost[i] ≤ 106
  • 1 ≤ time[i] ≤ 500

Visualization

Tap to expand
Painting the Walls - DP Approach INPUT cost[] array 1 2 3 2 index: 0 1 2 3 time[] array 1 2 3 2 index: 0 1 2 3 Two Painters: Paid Painter cost[i] money time[i] units Free Painter 0 cost 1 unit time n = 4 walls to paint Free painter works only when paid is busy ALGORITHM STEPS 1 Define DP State dp[i][j] = min cost for walls 0..i with j walls remaining 2 Transition Logic Skip: dp[i-1][j] Paint: cost[i]+dp[i-1][j-1-t[i]] 3 Key Observation Paid paints wall i, free can paint time[i] walls for free 4 Optimize to 1D DP dp[j] = min cost for j walls DP Table (simplified) j\i 0 1 2 3 0 0 0 0 0 4 inf inf 3 3 FINAL RESULT Optimal Strategy Wall 0 FREE $0 Wall 1 FREE $0 Wall 2 PAID $3 Wall 3 FREE $0 Paid painter on Wall 2: time[2] = 3 units Free painter can paint 3 walls (0, 1, 3) for free! Cost Breakdown Wall 2 (paid): $3 Walls 0,1,3 (free): $0 Total: $3 OUTPUT 3 OK Key Insight: When paid painter works on wall i for time[i] units, the free painter can paint time[i] other walls. This means choosing wall i "covers" (1 + time[i]) walls total. DP finds minimum cost to cover n walls. Time Complexity: O(n^2) | Space Complexity: O(n) with 1D DP optimization. TutorialsPoint - Painting the Walls | Dynamic Programming Approach
Asked in
Google 15 Amazon 12 Microsoft 8 Meta 6
31.2K Views
Medium Frequency
~35 min Avg. Time
892 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