forked from 7oSkaaa/LeetCode_DailyChallenge_2023
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch '7oSkaaa:main' into main
- Loading branch information
Showing
13 changed files
with
239 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
19 changes: 19 additions & 0 deletions
19
...mum Salary/01- Average Salary Excluding the Minimum and Maximum Salary (Ahmed Hossam).cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<int>& 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; | ||
} | ||
|
||
}; |
16 changes: 16 additions & 0 deletions
16
...m Salary/01- Average Salary Excluding the Minimum and Maximum Salary (Ibrahim Khalid).cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
// Author: Ibrahim Khalid | ||
class Solution { | ||
public: | ||
double average(vector<int>& 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.size()-1;i++){ | ||
sum+=salary[i]; | ||
} | ||
// divide on number salary excluding the minimum and maximum salary | ||
return sum/(salary.size()-2); | ||
|
||
} | ||
}; |
21 changes: 21 additions & 0 deletions
21
...alary/01- Average Salary Excluding the Minimum and Maximum Salary (Mahmoud Aboelsoud).cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
// Author: Mahmoud Aboelsoud | ||
|
||
class Solution { | ||
public: | ||
double average(vector<int>& 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); | ||
} | ||
}; |
20 changes: 20 additions & 0 deletions
20
...ximum Salary/01- Average Salary Excluding the Minimum and Maximum Salary (Omar Sanad).cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
// author : Omar Sanad | ||
class Solution { | ||
public: | ||
double average(vector<int>& 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)); | ||
} | ||
}; |
28 changes: 28 additions & 0 deletions
28
...aximum Salary/01- Average Salary Excluding the Minimum and Maximum Salary (Omar Wael).cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
// Author : Omar Wael | ||
// leetcode account : https://leetcode.com/OmarWael1/ | ||
|
||
class Solution { | ||
public: | ||
double average(vector<int>& salary) { | ||
int mn=INT_MAX,mx=0; | ||
double ave=0; | ||
for(auto i:salary){ | ||
// get min element | ||
if(i<mn){ | ||
mn=i; | ||
} | ||
// get max element | ||
if(i>mx){ | ||
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; | ||
} | ||
}; |
24 changes: 24 additions & 0 deletions
24
...mum Salary/01- Average Salary Excluding the Minimum and Maximum Salary (Osama Ayman).java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
... and Maximum Salary/01- Average Salary Excluding the Minimum and Maximum Salary (cpp).cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
// Author: Noura Algohary | ||
class Solution { | ||
public: | ||
double average(vector<int>& 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); | ||
} | ||
}; |
4 changes: 4 additions & 0 deletions
4
...um and Maximum Salary/01- Average Salary Excluding the Minimum and Maximum Salary (py).py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) |
22 changes: 22 additions & 0 deletions
22
... Sign of the Product of an Array/02- Sign of the Product of an Array (Noura Algohary).cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
// Author: Noura Algohary | ||
class Solution { | ||
public: | ||
int arraySign(vector<int>& 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; | ||
} | ||
}; |
14 changes: 14 additions & 0 deletions
14
...- Sign of the Product of an Array/02- Sign of the Product of an Array (Noura Algohary).py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
# LeetCode Daily Challenge Problems for May | ||
|
||
<br><br> | ||
|
||
## Workflow Checking | ||
|
||
<div align="center"> | ||
<img src="https://github.com/7oSkaaa/LeetCode_DailyChallenge_2023/actions/workflows/Author_Line.yml/badge.svg" alt="Checkers" width="150"> | ||
<a href="https://github.com/7oSkaaa/LeetCode_DailyChallenge_2023/actions/workflows/Author_Line.yml" taget="_blank"/> | ||
</img> | ||
| ||
<img src="https://github.com/7oSkaaa/LeetCode_DailyChallenge_2023/actions/workflows/File_Names.yml/badge.svg" alt="Checkers" width="150"> | ||
<a href="https://github.com/7oSkaaa/LeetCode_DailyChallenge_2023/actions/workflows/File_Names.yml" taget="_blank"/> | ||
</img> | ||
| ||
<img src="https://github.com/7oSkaaa/LeetCode_DailyChallenge_2023/actions/workflows/Daily_Problem.yml/badge.svg" alt="Checkers" width="150"> | ||
<a href="https://github.com/7oSkaaa/LeetCode_DailyChallenge_2023/actions/workflows/Daily_Problem.yml" taget="_blank"/> | ||
</img> | ||
</div> | ||
|
||
<br><br> | ||
|
||
## Problems: | ||
1. **[Average Salary Excluding the Minimum and Maximum Salary](#01--average-salary-excluding-the-minimum-and-maximum-salary)** | ||
|
||
<hr> | ||
<br><br> | ||
|
||
## 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<int>& 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; | ||
} | ||
|
||
}; | ||
``` | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters