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 0956d7b commit df978fc
Showing 1 changed file with 57 additions and 0 deletions.
57 changes: 57 additions & 0 deletions src/main/kotlin/p32xx/Problem3219.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package p32xx

import util.expect

fun main() {
class Solution {
fun minimumCost(m: Int, n: Int, horizontalCut: IntArray, verticalCut: IntArray): Long {
horizontalCut.sort()
verticalCut.sort()

var result = 0L

var hIndex = horizontalCut.lastIndex
var vIndex = verticalCut.lastIndex
while (hIndex >= 0 || vIndex >= 0) {
result += if (vIndex < 0 || (hIndex >= 0 && horizontalCut[hIndex] > verticalCut[vIndex])) {
horizontalCut[hIndex--] * (verticalCut.size - vIndex)
} else {
verticalCut[vIndex--] * (horizontalCut.size - hIndex)
}
}
return result
}
}

expect(13) {
Solution().minimumCost(
3, 2,
intArrayOf(1, 3),
intArrayOf(5),
)
}

expect(15) {
Solution().minimumCost(
2, 2,
intArrayOf(7),
intArrayOf(4),
)
}

expect(28) {
Solution().minimumCost(
6, 3,
intArrayOf(2, 3, 2, 3, 1),
intArrayOf(1, 2),
)
}

expect(258) {
Solution().minimumCost(
7, 4,
intArrayOf(13, 6, 12, 14, 4, 7),
intArrayOf(14, 15, 11),
)
}
}

0 comments on commit df978fc

Please sign in to comment.