All Downloads are FREE. Search and download functionalities are using the official Maven repository.

g2801_2900.s2865_beautiful_towers_i.Solution.kt Maven / Gradle / Ivy

There is a newer version: 1.30
Show newest version
package g2801_2900.s2865_beautiful_towers_i

// #Medium #Array #Stack #Monotonic_Stack #2023_12_21_Time_232_ms_(75.00%)_Space_38.8_MB_(75.00%)

import kotlin.math.max
import kotlin.math.min

class Solution {
    private fun `fun`(maxHeights: List, pickId: Int): Long {
        var ans = maxHeights[pickId].toLong()
        var min = maxHeights[pickId].toLong()
        for (i in pickId - 1 downTo 0) {
            min = min(min.toDouble(), maxHeights[i].toDouble()).toLong()
            ans += min
        }
        min = maxHeights[pickId].toLong()
        for (i in pickId + 1 until maxHeights.size) {
            min = min(min.toDouble(), maxHeights[i].toDouble()).toLong()
            ans += min
        }
        return ans
    }

    fun maximumSumOfHeights(maxHeights: List): Long {
        val n = maxHeights.size
        var ans: Long = 0
        for (i in 0 until n) {
            if (i == 0 || i == n - 1 || (
                maxHeights[i] >= maxHeights[i - 1] &&
                    maxHeights[i] >= maxHeights[i + 1]
                )
            ) {
                ans = max(ans.toDouble(), `fun`(maxHeights, i).toDouble()).toLong()
            }
        }
        return ans
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy