DMITRII’s Substack

Share this post

Leetcode daily # 1.05.2023

dmitriisamoilenko.substack.com

Leetcode daily # 1.05.2023

[1491. Average Salary Excluding the Minimum and Maximum Salary]

DMITRII SAMOILENKO
May 1, 2023
Share
Share this post

Leetcode daily # 1.05.2023

dmitriisamoilenko.substack.com

1.05.2023

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()

blog post

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)

Thanks for reading DMITRII’s Substack! Subscribe for free to receive new posts and support my work.

Share
Share this post

Leetcode daily # 1.05.2023

dmitriisamoilenko.substack.com
Previous
Next
Comments
Top
New

No posts

Ready for more?

© 2023 DMITRII SAMOILENKO
Privacy ∙ Terms ∙ Collection notice
Start WritingGet the app
Substack is the home for great writing