LeetCode in Kotlin

2742. Painting the Walls

Hard

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:

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:

Solution

class Solution {
    fun paintWalls(cost: IntArray, time: IntArray): Int {
        val n = cost.size
        val dp = Array(n + 1) { IntArray(n + 1) }
        return solve(n, cost, 0, time, dp)
    }

    private fun solve(wallsRem: Int, cost: IntArray, idx: Int, time: IntArray, dp: Array<IntArray>): Int {
        if (wallsRem <= 0) return 0
        if (idx >= cost.size) return 1000000000
        if (dp[idx][wallsRem] != 0) {
            return dp[idx][wallsRem]
        }
        val skip = solve(wallsRem, cost, idx + 1, time, dp)
        val take = cost[idx] + solve(wallsRem - time[idx] - 1, cost, idx + 1, time, dp)
        dp[idx][wallsRem] = skip.coerceAtMost(take)
        return dp[idx][wallsRem]
    }
}