Leetcode-2742

Painting the Walls

Time complexity: $O(n^2)$

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <iostream>
#include <vector>

const int INF = 0x3fff'ffff;

class Solution {
public:
    int paintWalls(std::vector<int>& cost, std::vector<int>& time) {
        int n = cost.size();
        std::vector<int> dp(n + 1, INF);
        dp[0] = 0;
        int j;
        
        for (int i = 0; i < n; ++i) {
            for (j = n; j > time[i]; --j) {
                dp[j] = std::min(dp[j], dp[j - time[i] - 1] + cost[i]);
            }
            for (; j > 0; --j) {
                dp[j] = std::min(dp[j], cost[i]);
            }
        }
        
        return dp.back();
    }
};

Leetcode-2742

Licensed under CC BY-NC-SA 4.0