g0501_0600.s0554_brick_wall.Solution.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of leetcode-in-kotlin Show documentation
Show all versions of leetcode-in-kotlin Show documentation
Kotlin-based LeetCode algorithm problem solutions, regularly updated
package g0501_0600.s0554_brick_wall
// #Medium #Array #Hash_Table #2023_01_17_Time_307_ms_(100.00%)_Space_47.5_MB_(90.00%)
class Solution {
fun leastBricks(wall: List>): Int {
val gapMap = mutableMapOf()
wall.forEach { row ->
var pos = 0
for (brickId in 0 until row.lastIndex) {
pos += row[brickId]
gapMap[pos] = gapMap.getOrDefault(pos, 0) + 1
}
}
var value = 0
if (gapMap.isNotEmpty()) {
value = gapMap.values.max()
}
return wall.size - value
}
}