|
| 1 | +# 2742. Painting the Walls |
| 2 | + |
| 3 | +- Difficulty: Hard. |
| 4 | +- Related Topics: Array, Dynamic Programming. |
| 5 | +- Similar Questions: . |
| 6 | + |
| 7 | +## Problem |
| 8 | + |
| 9 | +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: |
| 10 | + |
| 11 | + |
| 12 | + |
| 13 | +- A** paid painter** that paints the `ith` wall in `time[i]` units of time and takes `cost[i]` units of money. |
| 14 | + |
| 15 | +- 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**. |
| 16 | + |
| 17 | + |
| 18 | +Return **the minimum amount of money required to paint the **`n`** walls.** |
| 19 | + |
| 20 | + |
| 21 | +Example 1: |
| 22 | + |
| 23 | +``` |
| 24 | +Input: cost = [1,2,3,2], time = [1,2,3,2] |
| 25 | +Output: 3 |
| 26 | +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. |
| 27 | +``` |
| 28 | + |
| 29 | +Example 2: |
| 30 | + |
| 31 | +``` |
| 32 | +Input: cost = [2,3,4,2], time = [1,1,1,1] |
| 33 | +Output: 4 |
| 34 | +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. |
| 35 | +``` |
| 36 | + |
| 37 | + |
| 38 | +**Constraints:** |
| 39 | + |
| 40 | + |
| 41 | + |
| 42 | +- `1 <= cost.length <= 500` |
| 43 | + |
| 44 | +- `cost.length == time.length` |
| 45 | + |
| 46 | +- `1 <= cost[i] <= 106` |
| 47 | + |
| 48 | +- `1 <= time[i] <= 500` |
| 49 | + |
| 50 | + |
| 51 | + |
| 52 | +## Solution |
| 53 | + |
| 54 | +```javascript |
| 55 | +/** |
| 56 | + * @param {number[]} cost |
| 57 | + * @param {number[]} time |
| 58 | + * @return {number} |
| 59 | + */ |
| 60 | +var paintWalls = function(cost, time) { |
| 61 | + var dp = Array(cost.length).fill(0).map(() => Array(cost.length + 1)); |
| 62 | + return helper(cost, time, 0, cost.length, dp); |
| 63 | +}; |
| 64 | + |
| 65 | +var helper = function(cost, time, i, remains, dp) { |
| 66 | + if (remains <= 0) return 0; |
| 67 | + if (i === cost.length) return Number.MAX_SAFE_INTEGER; |
| 68 | + if (dp[i][remains] !== undefined) return dp[i][remains]; |
| 69 | + var paintByPaidPainter = cost[i] + helper(cost, time, i + 1, remains - time[i] - 1, dp); |
| 70 | + var paintByFreePainter = helper(cost, time, i + 1, remains, dp); |
| 71 | + dp[i][remains] = Math.min(paintByPaidPainter, paintByFreePainter); |
| 72 | + return dp[i][remains]; |
| 73 | +}; |
| 74 | +``` |
| 75 | + |
| 76 | +**Explain:** |
| 77 | + |
| 78 | +Top down dp. |
| 79 | + |
| 80 | +**Complexity:** |
| 81 | + |
| 82 | +* Time complexity : O(n ^ 2). |
| 83 | +* Space complexity : O(n ^ 2). |
0 commit comments