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

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

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

// #Medium #Array #Stack #Monotonic_Stack #2023_12_21_Time_676_ms_(85.71%)_Space_70.9_MB_(71.43%)

import java.util.Deque
import java.util.LinkedList
import kotlin.math.max

class Solution {
    fun maximumSumOfHeights(mH: List): Long {
        val n = mH.size
        val st: Deque = LinkedList()
        val prevSmaller = IntArray(n + 1)
        for (i in 0 until n) {
            while (st.isNotEmpty() && mH[st.peek()] >= mH[i]) {
                st.pop()
            }
            if (st.isEmpty()) {
                prevSmaller[i] = -1
            } else {
                prevSmaller[i] = st.peek()
            }
            st.push(i)
        }
        st.clear()
        val nextSmaller = IntArray(n + 1)
        for (i in n - 1 downTo 0) {
            while (st.isNotEmpty() && mH[st.peek()] >= mH[i]) {
                st.pop()
            }
            if (st.isEmpty()) {
                nextSmaller[i] = n
            } else {
                nextSmaller[i] = st.peek()
            }
            st.push(i)
        }
        val leftSum = LongArray(n)
        leftSum[0] = mH[0].toLong()
        for (i in 1 until n) {
            val prevSmallerIdx = prevSmaller[i]
            val equalCount = i - prevSmallerIdx
            leftSum[i] = (equalCount.toLong() * mH[i])
            if (prevSmallerIdx != -1) {
                leftSum[i] += leftSum[prevSmallerIdx]
            }
        }
        val rightSum = LongArray(n)
        rightSum[n - 1] = mH[n - 1].toLong()
        for (i in n - 2 downTo 0) {
            val nextSmallerIdx = nextSmaller[i]
            val equalCount = nextSmallerIdx - i
            rightSum[i] = (equalCount.toLong() * mH[i])
            if (nextSmallerIdx != n) {
                rightSum[i] += rightSum[nextSmallerIdx]
            }
        }
        var ans: Long = 0
        for (i in 0 until n) {
            val totalSum = leftSum[i] + rightSum[i] - mH[i]
            ans = max(ans, totalSum)
        }
        return ans
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy