vault backup: 2022-07-23 15:28:38
This commit is contained in:
parent
6f17f1002b
commit
5c66df3c0f
|
@ -0,0 +1,76 @@
|
|||
# Leetcode Average-Salary-Excluding-the-Minimum-and-Maximum-Salary
|
||||
|
||||
#### 2022-07-23 15:27
|
||||
|
||||
> ##### Difficulty:
|
||||
> #coding_problem #difficulty-easy
|
||||
> ##### Additional tags:
|
||||
> #leetcode #math
|
||||
> ##### Revisions:
|
||||
> N/A
|
||||
|
||||
##### Related topics:
|
||||
```expander
|
||||
tag:#math
|
||||
```
|
||||
|
||||
|
||||
|
||||
##### Links:
|
||||
- [Link to problem](https://leetcode.com/problems/average-salary-excluding-the-minimum-and-maximum-salary/)
|
||||
___
|
||||
### Problem
|
||||
|
||||
You are given an array of **unique** integers `salary` where `salary[i]` is the salary of the `ith` employee.
|
||||
|
||||
Return _the average salary of employees excluding the minimum and maximum salary_. Answers within `10-5` of the actual answer will be accepted.
|
||||
|
||||
#### Examples
|
||||
|
||||
**Example 1:**
|
||||
|
||||
**Input:** salary = [4000,3000,1000,2000]
|
||||
**Output:** 2500.00000
|
||||
**Explanation:** Minimum salary and maximum salary are 1000 and 4000 respectively.
|
||||
Average salary excluding minimum and maximum salary is (2000+3000) / 2 = 2500
|
||||
|
||||
**Example 2:**
|
||||
|
||||
**Input:** salary = [1000,2000,3000]
|
||||
**Output:** 2000.00000
|
||||
**Explanation:** Minimum salary and maximum salary are 1000 and 3000 respectively.
|
||||
Average salary excluding minimum and maximum salary is (2000) / 1 = 2000
|
||||
|
||||
#### Constraints
|
||||
|
||||
- `3 <= salary.length <= 100`
|
||||
- `1000 <= salary[i] <= 106`
|
||||
- All the integers of `salary` are **unique**.
|
||||
|
||||
### Thoughts
|
||||
|
||||
> [!summary]
|
||||
> This is a #math problem
|
||||
|
||||
The one solution I came up with is using math.
|
||||
|
||||
When counting total, be warned of integer overflows.
|
||||
|
||||
### Solution
|
||||
|
||||
```cpp
|
||||
class Solution {
|
||||
public:
|
||||
double average(vector<int> &salary) {
|
||||
double ans = 0, factor = salary.size() - 2;
|
||||
int maxSal = 0, minSal = 1000001;
|
||||
|
||||
for (int i = 0, size = factor + 2; i < size; i++) {
|
||||
ans += salary[i] / factor;
|
||||
maxSal = max(maxSal, salary[i]);
|
||||
minSal = min(minSal, salary[i]);
|
||||
}
|
||||
return ans - minSal / factor - maxSal / factor;
|
||||
}
|
||||
};
|
||||
```
|
|
@ -13,23 +13,53 @@
|
|||
|
||||
##### Related topics:
|
||||
```expander
|
||||
tag:#<INSERT_TAG_HERE>
|
||||
tag:#math
|
||||
```
|
||||
|
||||
|
||||
|
||||
##### Links:
|
||||
- [Link to problem]()
|
||||
- [Link to problem](https://leetcode.com/problems/count-odd-numbers-in-an-interval-range/)
|
||||
___
|
||||
### Problem
|
||||
|
||||
Given two non-negative integers `low` and `high`. Return the _count of odd numbers between_ `low` _and_ `high` _(inclusive)_.
|
||||
|
||||
#### Examples
|
||||
|
||||
**Example 1:**
|
||||
|
||||
**Input:** low = 3, high = 7
|
||||
**Output:** 3
|
||||
**Explanation:** The odd numbers between 3 and 7 are [3,5,7].
|
||||
|
||||
**Example 2:**
|
||||
|
||||
**Input:** low = 8, high = 10
|
||||
**Output:** 1
|
||||
**Explanation:** The odd numbers between 8 and 10 are [9].
|
||||
|
||||
#### Constraints
|
||||
|
||||
- `0 <= low <= high <= 10^9`
|
||||
|
||||
### Thoughts
|
||||
|
||||
> [!summary]
|
||||
> This is a #template_remove_me
|
||||
> This is a #math problem.
|
||||
|
||||
The problem is intended to be solved with math.
|
||||
|
||||
The key to find the solution is by analyzing and find common
|
||||
rules.
|
||||
|
||||
### Solution
|
||||
|
||||
```cpp
|
||||
class Solution {
|
||||
public:
|
||||
int countOdds(int low, int high) {
|
||||
return ((low % 2) | (high % 2)) + (high - low) / 2;
|
||||
}
|
||||
};
|
||||
```
|
Loading…
Reference in a new issue