Skip to content

Commit 551416d

Browse files
authored
Create 2742-painting-the-walls.kt
1 parent bfb2a1e commit 551416d

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

Diff for: kotlin/2742-painting-the-walls.kt

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
class Solution {
2+
fun paintWalls(cost: IntArray, time: IntArray): Int {
3+
val n = cost.size
4+
val dp = Array (n) { IntArray (n + 1) { -1 } }
5+
6+
fun dfs(i: Int, rem: Int): Int {
7+
if (rem <= 0) return 0
8+
if (i == n) return INFINITY
9+
if (dp[i][rem] != -1) return dp[i][rem]
10+
11+
dp[i][rem] = minOf(
12+
cost[i] + dfs(i + 1, rem - 1 - time[i]),
13+
dfs(i + 1, rem)
14+
)
15+
16+
return dp[i][rem]
17+
}
18+
19+
return dfs(0, n)
20+
}
21+
22+
companion object {
23+
const val INFINITY = 500000001
24+
}
25+
}

0 commit comments

Comments
 (0)