diff --git a/04- April/README.md b/04- April/README.md index 3fa254dcf..b34749c66 100644 --- a/04- April/README.md +++ b/04- April/README.md @@ -23,7 +23,6 @@ ## Problems: 1. **[Binary Search](#01--binary-search)** - 1. **[Successful Pairs of Spells and Potions](#02--successful-pairs-of-spells-and-potions)** 1. **[Boats to Save People](#03--boats-to-save-people)** 1. **[Optimal Partition of String](#04--optimal-partition-of-string)** diff --git a/05- May/01- Average Salary Excluding the Minimum and Maximum Salary/01- Average Salary Excluding the Minimum and Maximum Salary (Ahmed Hossam).cpp b/05- May/01- Average Salary Excluding the Minimum and Maximum Salary/01- Average Salary Excluding the Minimum and Maximum Salary (Ahmed Hossam).cpp new file mode 100644 index 000000000..b5991f45e --- /dev/null +++ b/05- May/01- Average Salary Excluding the Minimum and Maximum Salary/01- Average Salary Excluding the Minimum and Maximum Salary (Ahmed Hossam).cpp @@ -0,0 +1,19 @@ +// Author: Ahmed Hossam + +class Solution { +public: + // This function calculates the average of a vector of integers by sorting it and removing the minimum and maximum values. + double average(vector& salary) { + + // Calculate the sum of all the elements in the vector except for the minimum and maximum values. + double sum = 0, n = salary.size() - 2; + sort(salary.begin(), salary.end()); + for(auto& i : salary) + sum += i; + sum -= salary.front() + salary.back(); + + // Return the average by dividing the sum by the number of elements in the vector minus 2. + return sum / n; + } + +}; diff --git a/05- May/01- Average Salary Excluding the Minimum and Maximum Salary/01- Average Salary Excluding the Minimum and Maximum Salary (Ibrahim Khalid).cpp b/05- May/01- Average Salary Excluding the Minimum and Maximum Salary/01- Average Salary Excluding the Minimum and Maximum Salary (Ibrahim Khalid).cpp new file mode 100644 index 000000000..398498a8f --- /dev/null +++ b/05- May/01- Average Salary Excluding the Minimum and Maximum Salary/01- Average Salary Excluding the Minimum and Maximum Salary (Ibrahim Khalid).cpp @@ -0,0 +1,16 @@ +// Author: Ibrahim Khalid +class Solution { +public: + double average(vector& salary) { + // sort to excluding the minimum and maximum salary + sort(salary.begin(),salary.end()); + // store summation + long double sum=0; + for(int i=1;i& salary) { + // we need find the avaerage of all salaries except the minimum and maximum salaries + // we can find the minimum and maximum salaries using min_element and max_element + // then we can find the average of all salaries except the minimum and maximum salaries + + + // mn: minimum salary, mx: maximum salary + int mn = *min_element(salary.begin(), salary.end()), mx = *max_element(salary.begin(), salary.end()); + // sum: sum of all salaries except the minimum and maximum salaries + double sum = 0; + // loop over all salaries and add them to sum except the minimum and maximum salaries + for(auto&i: salary) sum += i * (i != mn && i != mx); + + // return the average of all salaries except the minimum and maximum salaries + return sum / (salary.size() - 2); + } +}; diff --git a/05- May/01- Average Salary Excluding the Minimum and Maximum Salary/01- Average Salary Excluding the Minimum and Maximum Salary (Omar Sanad).cpp b/05- May/01- Average Salary Excluding the Minimum and Maximum Salary/01- Average Salary Excluding the Minimum and Maximum Salary (Omar Sanad).cpp new file mode 100644 index 000000000..3d2d3a1eb --- /dev/null +++ b/05- May/01- Average Salary Excluding the Minimum and Maximum Salary/01- Average Salary Excluding the Minimum and Maximum Salary (Omar Sanad).cpp @@ -0,0 +1,20 @@ +// author : Omar Sanad +class Solution { +public: + double average(vector& salary) { + + // sort the array salary, to easily exclude the minimum and maximum element + sort(salary.begin(), salary.end()); + + // declare a variable to store the sum of all salaries excluding the min and max + int sum = 0; + + // iterate over all salaries except the min and max, then add up to "sum" + for (int i = 1; i < salary.size() - 1; i++) + sum += salary[i]; + + // return sum divided by the number of salaries I considered + // number of salaries I considered = the number of all salaries excluding the min and max + return sum / (1.0 * (salary.size() - 2)); + } +}; diff --git a/05- May/01- Average Salary Excluding the Minimum and Maximum Salary/01- Average Salary Excluding the Minimum and Maximum Salary (Omar Wael).cpp b/05- May/01- Average Salary Excluding the Minimum and Maximum Salary/01- Average Salary Excluding the Minimum and Maximum Salary (Omar Wael).cpp new file mode 100644 index 000000000..a66049b37 --- /dev/null +++ b/05- May/01- Average Salary Excluding the Minimum and Maximum Salary/01- Average Salary Excluding the Minimum and Maximum Salary (Omar Wael).cpp @@ -0,0 +1,28 @@ +// Author : Omar Wael +// leetcode account : https://leetcode.com/OmarWael1/ + +class Solution { +public: + double average(vector& salary) { + int mn=INT_MAX,mx=0; + double ave=0; + for(auto i:salary){ + // get min element + if(imx){ + mx=i; + } + // add current element to the sum + ave+=i; + } + // subtract min and max element from the sum + ave-=(mx+mn); + // devide the sum by the number of elements + ave/=salary.size()-2; + // return the average + return ave; + } +}; diff --git a/05- May/01- Average Salary Excluding the Minimum and Maximum Salary/01- Average Salary Excluding the Minimum and Maximum Salary (Osama Ayman).java b/05- May/01- Average Salary Excluding the Minimum and Maximum Salary/01- Average Salary Excluding the Minimum and Maximum Salary (Osama Ayman).java new file mode 100644 index 000000000..150cdb694 --- /dev/null +++ b/05- May/01- Average Salary Excluding the Minimum and Maximum Salary/01- Average Salary Excluding the Minimum and Maximum Salary (Osama Ayman).java @@ -0,0 +1,24 @@ +// Author: Osama Ayman +// Time: O(n) +// Space: O(1) +class Solution { + public double average(int[] salary) { + int max = -1, min = Integer.MAX_VALUE; + double res = 0; + for(int sal: salary){ + // updating max + max = Math.max(max, sal); + // updating min + min = Math.min(min, sal); + // adding all salaries + res += sal; + } + // removing the max + res -= max; + // removing the min + res -= min; + // calculating the average + res /= (salary.length-2); + return res; + } +} \ No newline at end of file diff --git a/05- May/01- Average Salary Excluding the Minimum and Maximum Salary/01- Average Salary Excluding the Minimum and Maximum Salary (cpp).cpp b/05- May/01- Average Salary Excluding the Minimum and Maximum Salary/01- Average Salary Excluding the Minimum and Maximum Salary (cpp).cpp new file mode 100644 index 000000000..4e06ee96a --- /dev/null +++ b/05- May/01- Average Salary Excluding the Minimum and Maximum Salary/01- Average Salary Excluding the Minimum and Maximum Salary (cpp).cpp @@ -0,0 +1,9 @@ +// Author: Noura Algohary +class Solution { +public: + double average(vector& salary) { + //accumulate returns the sum of elements of vector "salary" + + return double((accumulate(salary.begin(), salary.end(), 0) - *max_element(salary.begin(), salary.end()) - *min_element(salary.begin(), salary.end()))) / (salary.size() - 2); + } +}; \ No newline at end of file diff --git a/05- May/01- Average Salary Excluding the Minimum and Maximum Salary/01- Average Salary Excluding the Minimum and Maximum Salary (py).py b/05- May/01- Average Salary Excluding the Minimum and Maximum Salary/01- Average Salary Excluding the Minimum and Maximum Salary (py).py new file mode 100644 index 000000000..40a8c0cd4 --- /dev/null +++ b/05- May/01- Average Salary Excluding the Minimum and Maximum Salary/01- Average Salary Excluding the Minimum and Maximum Salary (py).py @@ -0,0 +1,4 @@ +# Author: Noura Algohary +class Solution: + def average(self, salary: List[int]) -> float: + return (sum(salary) - min(salary) - max(salary)) / (len(salary) - 2) \ No newline at end of file diff --git a/05- May/02- Sign of the Product of an Array/02- Sign of the Product of an Array (Noura Algohary).cpp b/05- May/02- Sign of the Product of an Array/02- Sign of the Product of an Array (Noura Algohary).cpp new file mode 100644 index 000000000..a7ebcfdb8 --- /dev/null +++ b/05- May/02- Sign of the Product of an Array/02- Sign of the Product of an Array (Noura Algohary).cpp @@ -0,0 +1,22 @@ +// Author: Noura Algohary +class Solution { +public: + int arraySign(vector& nums) { + int signCounter = 0; + + for(int num : nums) + { + // if one zero appears, the result is zero + if(num==0) + return 0; + else if(num<0) + signCounter++; + } + + // if negative numbers are of odd count, the result is negative + if (signCounter%2==0) + return 1; + else + return -1; + } +}; \ No newline at end of file diff --git a/05- May/02- Sign of the Product of an Array/02- Sign of the Product of an Array (Noura Algohary).py b/05- May/02- Sign of the Product of an Array/02- Sign of the Product of an Array (Noura Algohary).py new file mode 100644 index 000000000..7e83fd589 --- /dev/null +++ b/05- May/02- Sign of the Product of an Array/02- Sign of the Product of an Array (Noura Algohary).py @@ -0,0 +1,14 @@ +# Author: Noura Algohary +class Solution: + def arraySign(self, nums: List[int]) -> int: + signCounter = 0 + + for num in nums: + # if one zero appears, the result is zero + if num == 0: + return 0 + elif num < 0: + signCounter += 1 + + # if negative numbers are of odd count, the result is negative + return 1 if signCounter % 2 == 0 else -1 \ No newline at end of file diff --git a/05- May/README.md b/05- May/README.md new file mode 100644 index 000000000..2ca4c9ad3 --- /dev/null +++ b/05- May/README.md @@ -0,0 +1,61 @@ +# LeetCode Daily Challenge Problems for May + +

+ +## Workflow Checking + + + +

+ +## Problems: +1. **[Average Salary Excluding the Minimum and Maximum Salary](#01--average-salary-excluding-the-minimum-and-maximum-salary)** + +
+

+ +## 01) [Average Salary Excluding the Minimum and Maximum Salary](https://leetcode.com/problems/average-salary-excluding-the-minimum-and-maximum-salary/) + +### Difficulty + +![](https://img.shields.io/badge/Easy-green?style=for-the-badge) + +### Related Topic + +`Array` `Sorting` + +### Code + + +```cpp +class Solution { +public: + // This function calculates the average of a vector of integers by sorting it and removing the minimum and maximum values. + double average(vector& salary) { + + // Calculate the sum of all the elements in the vector except for the minimum and maximum values. + double sum = 0, n = salary.size() - 2; + sort(salary.begin(), salary.end()); + for(auto& i : salary) + sum += i; + sum -= salary.front() + salary.back(); + + // Return the average by dividing the sum by the number of elements in the vector minus 2. + return sum / n; + } + +}; +``` + diff --git a/README.md b/README.md index 46dcbc1bd..dd02d8d60 100644 --- a/README.md +++ b/README.md @@ -48,4 +48,5 @@ https://user-images.githubusercontent.com/94416115/210334779-16a94ab4-5c24-4a90- - [**February**](https://github.com/7oSkaaa/LeetCode_DailyChallenge_2023/tree/main/02-%20February) - [**March**](https://github.com/7oSkaaa/LeetCode_DailyChallenge_2023/tree/main/03-%20March) - [**April**](https://github.com/7oSkaaa/LeetCode_DailyChallenge_2023/tree/main/04-%20April) +- [**May**](https://github.com/7oSkaaa/LeetCode_DailyChallenge_2023/tree/main/05-%20May)