Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Break down a hard problem into reliable checkpoints, edge-case handling, and complexity trade-offs.
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:
ith wall in time[i] units of time and takes cost[i] units of money.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.
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.
Constraints:
1 <= cost.length <= 500cost.length == time.length1 <= cost[i] <= 1061 <= time[i] <= 500Problem summary: 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.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Dynamic Programming
[1,2,3,2] [1,2,3,2]
[2,3,4,2] [1,1,1,1]
Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #2742: Painting the Walls
class Solution {
private int n;
private int[] cost;
private int[] time;
private Integer[][] f;
public int paintWalls(int[] cost, int[] time) {
n = cost.length;
this.cost = cost;
this.time = time;
f = new Integer[n][n << 1 | 1];
return dfs(0, n);
}
private int dfs(int i, int j) {
if (n - i <= j - n) {
return 0;
}
if (i >= n) {
return 1 << 30;
}
if (f[i][j] == null) {
f[i][j] = Math.min(dfs(i + 1, j + time[i]) + cost[i], dfs(i + 1, j - 1));
}
return f[i][j];
}
}
// Accepted solution for LeetCode #2742: Painting the Walls
func paintWalls(cost []int, time []int) int {
n := len(cost)
f := make([][]int, n)
for i := range f {
f[i] = make([]int, n<<1|1)
for j := range f[i] {
f[i][j] = -1
}
}
var dfs func(i, j int) int
dfs = func(i, j int) int {
if n-i <= j-n {
return 0
}
if i >= n {
return 1 << 30
}
if f[i][j] == -1 {
f[i][j] = min(dfs(i+1, j+time[i])+cost[i], dfs(i+1, j-1))
}
return f[i][j]
}
return dfs(0, n)
}
# Accepted solution for LeetCode #2742: Painting the Walls
class Solution:
def paintWalls(self, cost: List[int], time: List[int]) -> int:
@cache
def dfs(i: int, j: int) -> int:
if n - i <= j:
return 0
if i >= n:
return inf
return min(dfs(i + 1, j + time[i]) + cost[i], dfs(i + 1, j - 1))
n = len(cost)
return dfs(0, 0)
// Accepted solution for LeetCode #2742: Painting the Walls
impl Solution {
#[allow(dead_code)]
pub fn paint_walls(cost: Vec<i32>, time: Vec<i32>) -> i32 {
let n = cost.len();
let mut record_vec: Vec<Vec<i32>> = vec![vec![-1; n << 1 | 1]; n];
Self::dfs(&mut record_vec, 0, n as i32, n as i32, &time, &cost)
}
#[allow(dead_code)]
fn dfs(
record_vec: &mut Vec<Vec<i32>>,
i: i32,
j: i32,
n: i32,
time: &Vec<i32>,
cost: &Vec<i32>,
) -> i32 {
if n - i <= j - n {
// All the remaining walls can be printed at no cost
// Just return 0
return 0;
}
if i >= n {
// No way this case can be achieved
// Just return +INF
return 1 << 30;
}
if record_vec[i as usize][j as usize] == -1 {
// This record hasn't been written
record_vec[i as usize][j as usize] = std::cmp::min(
Self::dfs(record_vec, i + 1, j + time[i as usize], n, time, cost)
+ cost[i as usize],
Self::dfs(record_vec, i + 1, j - 1, n, time, cost),
);
}
record_vec[i as usize][j as usize]
}
}
// Accepted solution for LeetCode #2742: Painting the Walls
// Auto-generated TypeScript example from java.
function exampleSolution(): void {
}
// Reference (java):
// // Accepted solution for LeetCode #2742: Painting the Walls
// class Solution {
// private int n;
// private int[] cost;
// private int[] time;
// private Integer[][] f;
//
// public int paintWalls(int[] cost, int[] time) {
// n = cost.length;
// this.cost = cost;
// this.time = time;
// f = new Integer[n][n << 1 | 1];
// return dfs(0, n);
// }
//
// private int dfs(int i, int j) {
// if (n - i <= j - n) {
// return 0;
// }
// if (i >= n) {
// return 1 << 30;
// }
// if (f[i][j] == null) {
// f[i][j] = Math.min(dfs(i + 1, j + time[i]) + cost[i], dfs(i + 1, j - 1));
// }
// return f[i][j];
// }
// }
Use this to step through a reusable interview workflow for this problem.
Pure recursion explores every possible choice at each step. With two choices per state (take or skip), the decision tree has 2ⁿ leaves. The recursion stack uses O(n) space. Many subproblems are recomputed exponentially many times.
Each cell in the DP table is computed exactly once from previously solved subproblems. The table dimensions determine both time and space. Look for the state variables — each unique combination of state values is one cell. Often a rolling array can reduce space by one dimension.
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Wrong move: An incomplete state merges distinct subproblems and caches incorrect answers.
Usually fails on: Correctness breaks on cases that differ only in hidden state.
Fix: Define state so each unique subproblem maps to one DP cell.