LeetCode Entry
1491. Average Salary Excluding the Minimum and Maximum Salary
We can do .fold and iterate only once, but sum, max and min operators are less verbose.
1491. Average Salary Excluding the Minimum and Maximum Salary easy
fun average(salary: IntArray): Double = with (salary) {
(sum() - max()!! - min()!!) / (size - 2).toDouble()
}
or
fun average(salary: IntArray): Double = salary.sorted().drop(1).dropLast(1).average()
Join me on Telegram
https://t.me/leetcode_daily_unstoppable/198
Intuition
Just do what is asked.
Approach
We can do .fold and iterate only once, but sum, max and min operators are less verbose.
We also can sort it, that will make code even shorter.
Complexity
- Time complexity: \(O(n)\), \(O(nlog(n))\) for sorted
- Space complexity: \(O(1)\)