https://leetcode.com/problems/final-prices-with-a-special-discount-in-a-shop/ Easy

Решение

class Solution {
    fun finalPrices(prices: IntArray): IntArray {
        val stack = ArrayDeque<Int>()
        val result = prices.copyOf()

        for (i in prices.indices) {
            while (stack.isNotEmpty() && prices[stack.last()] >= prices[i]) {
                val idx = stack.removeLast()
                result[idx] -= prices[i]
            }
            stack.addLast(i)
        }

        return result
    }
}