From 5c66df3c0ff4aebba8cb225e6ac600c9dfc0f7d3 Mon Sep 17 00:00:00 2001 From: juan Date: Sat, 23 Jul 2022 15:28:38 +0800 Subject: [PATCH] vault backup: 2022-07-23 15:28:38 --- ...xcluding-the-Minimum-and-Maximum-Salary.md | 76 +++++++++++++++++++ ... Count-Odd-Numbers-in-an-Interval-Range.md | 36 ++++++++- 2 files changed, 109 insertions(+), 3 deletions(-) create mode 100644 OJ notes/pages/Leetcode Average-Salary-Excluding-the-Minimum-and-Maximum-Salary.md diff --git a/OJ notes/pages/Leetcode Average-Salary-Excluding-the-Minimum-and-Maximum-Salary.md b/OJ notes/pages/Leetcode Average-Salary-Excluding-the-Minimum-and-Maximum-Salary.md new file mode 100644 index 0000000..bd3afc0 --- /dev/null +++ b/OJ notes/pages/Leetcode Average-Salary-Excluding-the-Minimum-and-Maximum-Salary.md @@ -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 &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; + } +}; +``` \ No newline at end of file diff --git a/OJ notes/pages/Leetcode Count-Odd-Numbers-in-an-Interval-Range.md b/OJ notes/pages/Leetcode Count-Odd-Numbers-in-an-Interval-Range.md index 27fffa1..22efae6 100644 --- a/OJ notes/pages/Leetcode Count-Odd-Numbers-in-an-Interval-Range.md +++ b/OJ notes/pages/Leetcode Count-Odd-Numbers-in-an-Interval-Range.md @@ -13,23 +13,53 @@ ##### Related topics: ```expander -tag:# +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; + } +}; +``` \ No newline at end of file