Skip to content

Commit

Permalink
Merge branch '7oSkaaa:main' into main
Browse files Browse the repository at this point in the history
  • Loading branch information
AhmedGamal2212 committed May 2, 2023
2 parents d9663c3 + 9b3f3f5 commit 9606275
Show file tree
Hide file tree
Showing 13 changed files with 239 additions and 1 deletion.
1 change: 0 additions & 1 deletion 04- April/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)**
Expand Down
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;
}

};
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);

}
};
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);
}
};
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));
}
};
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;
}
};
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;
}
}
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);
}
};
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)
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;
}
};
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
61 changes: 61 additions & 0 deletions 05- May/README.md
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>
&nbsp;
<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>
&nbsp;
<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;
}

};
```

1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
<hr>

0 comments on commit 9606275

Please sign in to comment.