Painting the Walls

Hard Topics Company Tags

There are n walls and two painters. The paid painter needs time[i] time units and charges cost[i] to paint wall i. The free painter can paint any wall in one time unit, but may work only while the paid painter is busy. Each painter handles one wall at a time. Choose which walls each painter handles and return the minimum total cost to finish all walls.

Example 1:

Input: cost = [5,1,4]
time = [1,2,1]

Output: 1

Explanation: Pay 1 for wall 1, which takes two time units. During that time, the free painter finishes the other two walls. A total cost below 1 is impossible.

Example 2:

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

Output: 3

Explanation: Pay for walls 2 and 3 at a total cost of 2 + 1 = 3. Their two time units let the free painter finish walls 0 and 1. One paid wall would cover only two walls in total.

Example 3:

Input: cost = [7]
time = [500]

Output: 7

Explanation: The only wall must be painted by the paid painter, for cost 7. The free painter cannot work while the paid painter is idle.

Constraints:

  • 1 <= cost.length <= 500
  • cost.length == time.length
  • 1 <= cost[i] <= 1000000
  • 1 <= time[i] <= 500


Topics

Company Tags

Please upgrade to NeetCode Pro to view company tags.