LeetCode solutions

2808. Painting the Walls

My accepted C++ solution to LeetCode problem 2808, Painting the Walls, running in 19ms.

  • Difficulty: Hard
  • C++
  • Runtime 19ms
  • Memory 95.6MB

Read the problem on LeetCode View on GitHub

C++

Accepted on LeetCode — runtime 19ms, memory 95.6MB, accepted 2025-12-27.

cpp
class Solution {
    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 j = n; j >= 0; j--) {
                dp[min(n, j + time[i] + 1)] = min(dp[min(n, j + time[i] + 1)], dp[j] + cost[i]);
            }
        }
        return dp[n];
    }
};

Source