Skip to content

Commit

Permalink
1
Browse files Browse the repository at this point in the history
  • Loading branch information
isaiahliu committed Dec 25, 2024
1 parent 5aa97ea commit 977a905
Showing 1 changed file with 51 additions and 0 deletions.
51 changes: 51 additions & 0 deletions src/main/kotlin/p32xx/Problem3218.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package p32xx

import util.expect

fun main() {
class Solution {
fun minimumCost(m: Int, n: Int, horizontalCut: IntArray, verticalCut: IntArray): Int {
val cache = hashMapOf<Pair<Pair<Int, Int>, Pair<Int, Int>>, Int>()

fun dfs(hStart: Int, hEnd: Int, vStart: Int, vEnd: Int): Int {
val cacheKey = (hStart to hEnd) to (vStart to vEnd)

return when {
hStart > hEnd && vStart > vEnd -> 0
cacheKey in cache -> cache[cacheKey] ?: 0
else -> {
var min = Int.MAX_VALUE

(hStart..hEnd).forEach {
min = minOf(min, horizontalCut[it] + dfs(hStart, it - 1, vStart, vEnd) + dfs(it + 1, hEnd, vStart, vEnd))
}
(vStart..vEnd).forEach {
min = minOf(min, verticalCut[it] + dfs(hStart, hEnd, vStart, it - 1) + dfs(hStart, hEnd, it + 1, vEnd))
}

cache[cacheKey] = min
min
}
}
}

return dfs(0, horizontalCut.lastIndex, 0, verticalCut.lastIndex)
}
}

expect {
Solution().minimumCost(
4, 5,
intArrayOf(0, 3),
intArrayOf(0, 2),
)
}
// expect {
// Solution().shortestDistanceAfterQueries(
// 100000,
// Array(100000 - 1) {
// intArrayOf(0, it + 1)
// }
// )
// }
}

0 comments on commit 977a905

Please sign in to comment.