average-salary.dart (675B)
1 //Find the mean value of the list excluding the largest and smallest values. 2 //I solved this by sorting the list then iterating through excluding the first and last values. 3 //The time complexity of this code is O(nlog(n) where n is the number of values in the list. 4 //To optimize this solution I will complete it again iterating through the list one time instead of sorting first. 5 //Time: 268ms Beats: 57.69% 6 //Memory: 141.4MB Beats: 61.54% 7 8 class Solution { 9 double average(List<int> salary) { 10 salary.sort(); 11 int total = 0; 12 for(int i = 1 ; i < salary.length - 1 ; ++i){ 13 total += salary[i]; 14 } 15 16 return total / (salary.length - 2); 17 } 18 }