#2742

Painting the Walls

candidate master · 1455 · lc hard +32 · verified · 48.9% accepted · 1,493 likes · top 35%

Description

You have n walls and two painters. The paid painter takes time[i] to paint wall i at cost[i]; while the paid painter is busy, a free painter can paint any other wall in 1 unit at no cost. Return the minimum money to paint all n walls.

Example 1:

Input: cost = [1,2,3,2], time = [1,2,3,2]
Output: 3
Explanation: The walls at index 0 and 1 will be painted by the paid painter, and it will take 3 units of time; meanwhile, the free painter will paint the walls at index 2 and 3, free of cost in 2 units of time. Thus, the total cost is 1 + 2 = 3.

Example 2:

Input: cost = [2,3,4,2], time = [1,1,1,1]
Output: 4
Explanation: The walls at index 0 and 3 will be painted by the paid painter, and it will take 2 units of time; meanwhile, the free painter will paint the walls at index 1 and 2, free of cost in 2 units of time. Thus, the total cost is 2 + 2 = 4.

Code

1
2
3