LeetCode Entry

1887. Reduction Operations to Make the Array Elements Equal

19.11.2023 medium 2023 kotlin

Number of operations to decrease all elements to the next smallest

1887. Reduction Operations to Make the Array Elements Equal medium blog post substack image.png

Join me on Telegram

https://t.me/leetcode_daily_unstoppable/410

Problem TLDR

Number of operations to decrease all elements to the next smallest

Intuition

The algorithm pretty much in a problem definition, just implement it.

Approach

  • iterate from the second position, to simplify the initial conditions

Complexity

  • Time complexity: \(O(nlog())\)

  • Space complexity: \(O(n)\)

Code


    fun reductionOperations(nums: IntArray): Int {
      nums.sort()
      return (nums.size - 2 downTo 0).sumBy {
        if (nums[it] < nums[it + 1]) nums.size - 1 - it else 0
      }
    }