Find the minimum cost to paint all walls with a paid painter and a free painter

Quick Overview

Given the cost and painting time of each wall, find the minimum money needed to paint every wall when a free painter can paint one wall per time unit, but only while a paid painter is busy. It tests turning a scheduling rule into a subset selection problem and solving it with dynamic programming.

Find the minimum cost to paint all walls with a paid painter and a free painter

Company: Deshaw

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: hard

Interview Round: Other

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. There are two painters available: - A paid painter that paints the i-th 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. The free painter can only be used while the paid painter is already occupied. Return the minimum amount of money required to paint all `n` walls. ### Function Signature ```python def paint_walls(cost: list[int], time: list[int]) -> int: ``` ### Rules - Every wall is painted by exactly one of the two painters. - The two painters work at the same time. The paid painter paints its walls one after another. The free painter paints one wall per time unit, and only during time units in which the paid painter is busy. - Only money counts toward the answer. The total time taken does not matter. ### Constraints - `1 <= cost.length <= 500` - `cost.length == time.length` - `1 <= cost[i] <= 10^6` - `1 <= time[i] <= 500` ### Examples ```text Input: cost = [1,2,3,2], time = [1,2,3,2] Output: 3 ``` The paid painter paints walls 0 and 1 for 1 + 2 = 3 units of money, which keeps it busy for 1 + 2 = 3 time units. The free painter paints walls 2 and 3 during that time. ```text Input: cost = [2,3,4,2], time = [1,1,1,1] Output: 4 ``` The paid painter paints walls 0 and 3 for 2 + 2 = 4, which takes 2 time units, and the free painter paints walls 1 and 2 in that window. Paying for only one wall gives the free painter 1 time unit, which is not enough for the other 3 walls. ```text Input: cost = [7], time = [3] Output: 7 ``` The free painter cannot work unless the paid painter is busy, so the only wall has to go to the paid painter.

Overview: Given the cost and painting time of each wall, find the minimum money needed to paint every wall when a free painter can paint one wall per time unit, but only while a paid painter is busy. It tests turning a scheduling rule into a subset selection problem and solving it with dynamic programming.

Deshaw logo
Deshaw
Nov 24, 2026
hardSoftware EngineerOtherCoding & Algorithms
0
0

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. There are two painters available:

  • A paid painter that paints the i-th 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. The free painter can only be used while the paid painter is already occupied.

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

Function Signature

def paint_walls(cost: list[int], time: list[int]) -> int:

Rules

  • Every wall is painted by exactly one of the two painters.
  • The two painters work at the same time. The paid painter paints its walls one after another. The free painter paints one wall per time unit, and only during time units in which the paid painter is busy.
  • Only money counts toward the answer. The total time taken does not matter.

Constraints

  • 1 <= cost.length <= 500
  • cost.length == time.length
  • 1 <= cost[i] <= 10^6
  • 1 <= time[i] <= 500

Examples

Input: cost = [1,2,3,2], time = [1,2,3,2]
Output: 3

The paid painter paints walls 0 and 1 for 1 + 2 = 3 units of money, which keeps it busy for 1 + 2 = 3 time units. The free painter paints walls 2 and 3 during that time.

Input: cost = [2,3,4,2], time = [1,1,1,1]
Output: 4

The paid painter paints walls 0 and 3 for 2 + 2 = 4, which takes 2 time units, and the free painter paints walls 1 and 2 in that window. Paying for only one wall gives the free painter 1 time unit, which is not enough for the other 3 walls.

Input: cost = [7], time = [3]
Output: 7

The free painter cannot work unless the paid painter is busy, so the only wall has to go to the paid painter.