| | 1 | | // -------------------------------------------------------------------------------- |
| | 2 | | // Copyright (C) 2025 Eugene Eremeev (also known as Yevhenii Yeriemeieiv). |
| | 3 | | // All Rights Reserved. |
| | 4 | | // -------------------------------------------------------------------------------- |
| | 5 | | // This software is the confidential and proprietary information of Eugene Eremeev |
| | 6 | | // (also known as Yevhenii Yeriemeieiv) ("Confidential Information"). You shall not |
| | 7 | | // disclose such Confidential Information and shall use it only in accordance with |
| | 8 | | // the terms of the license agreement you entered into with Eugene Eremeev (also |
| | 9 | | // known as Yevhenii Yeriemeieiv). |
| | 10 | | // -------------------------------------------------------------------------------- |
| | 11 | |
|
| | 12 | | namespace LeetCode.Algorithms.AverageSalaryExcludingTheMinimumAndMaximumSalary; |
| | 13 | |
|
| | 14 | | /// <inheritdoc /> |
| | 15 | | public class AverageSalaryExcludingTheMinimumAndMaximumSalaryIterative : |
| | 16 | | IAverageSalaryExcludingTheMinimumAndMaximumSalary |
| | 17 | | { |
| | 18 | | /// <summary> |
| | 19 | | /// Time complexity - O(n) |
| | 20 | | /// Space complexity - O(1) |
| | 21 | | /// </summary> |
| | 22 | | /// <param name="salary"></param> |
| | 23 | | /// <returns></returns> |
| | 24 | | public double Average(int[] salary) |
| 2 | 25 | | { |
| 2 | 26 | | var min = salary[0]; |
| 2 | 27 | | var max = salary[0]; |
| | 28 | |
|
| 2 | 29 | | double sum = 0; |
| | 30 | |
|
| 20 | 31 | | foreach (var item in salary) |
| 7 | 32 | | { |
| 7 | 33 | | sum += item; |
| | 34 | |
|
| 7 | 35 | | min = Math.Min(min, item); |
| 7 | 36 | | max = Math.Max(max, item); |
| 7 | 37 | | } |
| | 38 | |
|
| 2 | 39 | | return (sum - min - max) / (salary.Length - 2); |
| 2 | 40 | | } |
| | 41 | | } |