leetcode

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README

commit be0a5d399a6820370f75e37b6adeb11b030b98c9
parent dfe2a87c2b21fbaa2306b426a560fa3f82e3ea25
Author: AndrewLockVI <andrewlaack1@gmail.com>
Date:   Mon,  1 May 2023 11:26:32 -0500

Completed average salary question with optimal solution

Diffstat:
Aaverage-salary-excluding-min-and-max/average-salaryV2.dart | 25+++++++++++++++++++++++++
1 file changed, 25 insertions(+), 0 deletions(-)

diff --git a/average-salary-excluding-min-and-max/average-salaryV2.dart b/average-salary-excluding-min-and-max/average-salaryV2.dart @@ -0,0 +1,25 @@ +//Better solution than sort solution where you iteate through +//the list one time tracking the highest and lowest vals. +//Then at the end subtract those from the total and return the mean. +//The time complexity of this code is O(n) where n is the length of the list +//Time: 260ms Beats: 80.77% +//Memory: 141.9MB Beats: 30.77% +class Solution { + double average(List<int> salary) { + int lowest = salary[0]; + int highest = salary[0]; + int total = 0; + for(int i = 0 ; i < salary.length ; ++i){ + if(salary[i] < lowest){ + lowest = salary[i]; + } + else if(salary[i] > highest){ + highest = salary[i]; + } + total += salary[i]; + } + total = total - (highest + lowest); + + return total / (salary.length - 2); + } +}