![JAR search and dependency download from the Maven repository](/logo.png)
g1901_2000.s1936_add_minimum_number_of_rungs.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 g1901_2000.s1936_add_minimum_number_of_rungs
// #Medium #Array #Greedy #2023_06_20_Time_405_ms_(100.00%)_Space_50.7_MB_(100.00%)
class Solution {
fun addRungs(rungs: IntArray, dist: Int): Int {
var addons = 0
var currentHeight = 0
var i = 0
while (i < rungs.size) {
val nextRung = rungs[i]
if (nextRung - currentHeight <= dist) {
currentHeight = nextRung
i++
} else {
val adds = (nextRung - currentHeight - 1) / dist
addons += adds
currentHeight += dist * adds
}
}
return addons
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy