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 thenwalls.
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:
1
2
3
4
5
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.
For each wall, for each possible number of walls painted, update dp by considering painting the current wall with the paid painter (which allows the free painter to paint up to time[i] walls for free).
The answer is dp[n], the minimum cost to paint all n walls.
classSolution {
public:int paintWalls(vector<int>& cost, vector<int>& time) {
int n = cost.size();
vector<int> dp(n+1, 1e9);
dp[0] =0;
for (int i =0; i < n; ++i) {
for (int k = n; k >=0; --k) {
int nk = min(n, k + time[i] +1);
dp[nk] = min(dp[nk], dp[k] + cost[i]);
}
}
return dp[n];
}
};
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
funcpaintWalls(cost []int, time []int) int {
n:= len(cost)
dp:= make([]int, n+1)
fori:=rangedp { dp[i] = 1e9 }
dp[0] = 0fori:=0; i < n; i++ {
fork:=n; k>=0; k-- {
nk:=k+time[i] +1ifnk > n { nk = n }
ifdp[k]+cost[i] < dp[nk] {
dp[nk] = dp[k]+cost[i]
}
}
}
returndp[n]
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
classSolution {
publicintpaintWalls(int[] cost, int[] time) {
int n = cost.length;
int[] dp =newint[n+1];
Arrays.fill(dp, (int)1e9);
dp[0]= 0;
for (int i = 0; i < n; ++i) {
for (int k = n; k >= 0; --k) {
int nk = Math.min(n, k + time[i]+ 1);
dp[nk]= Math.min(dp[nk], dp[k]+ cost[i]);
}
}
return dp[n];
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
classSolution {
funpaintWalls(cost: IntArray, time: IntArray): Int {
val n = cost.size
val dp = IntArray(n+1) { 1_000_000_000 }
dp[0] = 0for (i in0 until n) {
for (k in n downTo 0) {
val nk = minOf(n, k + time[i] + 1)
dp[nk] = minOf(dp[nk], dp[k] + cost[i])
}
}
return dp[n]
}
}
1
2
3
4
5
6
7
8
9
10
classSolution:
defpaintWalls(self, cost: list[int], time: list[int]) -> int:
n = len(cost)
dp = [float('inf')] * (n+1)
dp[0] =0for i in range(n):
for k in range(n, -1, -1):
nk = min(n, k + time[i] +1)
dp[nk] = min(dp[nk], dp[k] + cost[i])
return dp[n]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
impl Solution {
pubfnpaint_walls(cost: Vec<i32>, time: Vec<i32>) -> i32 {
let n = cost.len();
letmut dp =vec![i32::MAX; n+1];
dp[0] =0;
for i in0..n {
for k in (0..=n).rev() {
let nk = std::cmp::min(n, k + time[i] asusize+1);
dp[nk] = dp[nk].min(dp[k] + cost[i]);
}
}
dp[n]
}
}