diff --git "a/Index/\346\250\241\346\213\237.md" "b/Index/\346\250\241\346\213\237.md" index 35903b67..41907195 100644 --- "a/Index/\346\250\241\346\213\237.md" +++ "b/Index/\346\250\241\346\213\237.md" @@ -169,6 +169,7 @@ | [1606. 找到处理最多请求的服务器](https://leetcode-cn.com/problems/find-servers-that-handled-most-number-of-requests/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/find-servers-that-handled-most-number-of-requests/solution/by-ac_oier-zgm6/) | 困难 | 🤩🤩🤩🤩 | | [1608. 特殊数组的特征值](https://leetcode.cn/problems/special-array-with-x-elements-greater-than-or-equal-x/) | [LeetCode 题解链接](https://leetcode.cn/problems/special-array-with-x-elements-greater-than-or-equal-x/solution/by-ac_oier-z525/) | 简单 | 🤩🤩🤩🤩🤩 | | [1614. 括号的最大嵌套深度](https://leetcode-cn.com/problems/maximum-nesting-depth-of-the-parentheses/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/maximum-nesting-depth-of-the-parentheses/solution/gong-shui-san-xie-jian-dan-mo-ni-ti-by-a-pf5d/) | 简单 | 🤩🤩🤩🤩🤩 | +| [1619. 删除某些元素后的数组均值](https://leetcode.cn/problems/mean-of-array-after-removing-some-elements/) | [LeetCode 题解链接](https://leetcode.cn/problems/mean-of-array-after-removing-some-elements/solution/by-ac_oier-73w7/) | 简单 | 🤩🤩🤩🤩 | | [1629. 按键持续时间最长的键](https://leetcode-cn.com/problems/slowest-key/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/slowest-key/solution/gong-shui-san-xie-jian-dan-mo-ni-ti-by-a-zjwb/) | 简单 | 🤩🤩🤩🤩🤩 | | [1646. 获取生成数组中的最大值](https://leetcode-cn.com/problems/get-maximum-in-generated-array/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/get-maximum-in-generated-array/solution/gong-shui-san-xie-jian-dan-mo-ni-ti-by-a-sj53/) | 简单 | 🤩🤩🤩🤩 | | [1656. 设计有序流](https://leetcode.cn/problems/design-an-ordered-stream/) | [LeetCode 题解链接](https://leetcode.cn/problems/design-an-ordered-stream/solution/by-ac_oier-5pe8/) | 简单 | 🤩🤩🤩🤩 | diff --git "a/LeetCode/1611-1620/1619. \345\210\240\351\231\244\346\237\220\344\272\233\345\205\203\347\264\240\345\220\216\347\232\204\346\225\260\347\273\204\345\235\207\345\200\274\357\274\210\347\256\200\345\215\225\357\274\211.md" "b/LeetCode/1611-1620/1619. \345\210\240\351\231\244\346\237\220\344\272\233\345\205\203\347\264\240\345\220\216\347\232\204\346\225\260\347\273\204\345\235\207\345\200\274\357\274\210\347\256\200\345\215\225\357\274\211.md" new file mode 100644 index 00000000..f3e17ab5 --- /dev/null +++ "b/LeetCode/1611-1620/1619. \345\210\240\351\231\244\346\237\220\344\272\233\345\205\203\347\264\240\345\220\216\347\232\204\346\225\260\347\273\204\345\235\207\345\200\274\357\274\210\347\256\200\345\215\225\357\274\211.md" @@ -0,0 +1,91 @@ +### 题目描述 + +这是 LeetCode 上的 **[1619. 删除某些元素后的数组均值](https://leetcode.cn/problems/mean-of-array-after-removing-some-elements/solution/by-ac_oier-73w7/)** ,难度为 **简单**。 + +Tag : 「模拟」、「排序」 + + + +给你一个整数数组 `arr`,请你删除最小 `5%` 的数字和最大 `5%` 的数字后,剩余数字的平均值。 + +与 标准答案 误差在 $10^{-5}$ 的结果都被视为正确结果。 + +示例 1: +``` +输入:arr = [1,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,3] + +输出:2.00000 + +解释:删除数组中最大和最小的元素后,所有元素都等于 2,所以平均值为 2 。 +``` +示例 2: +``` +输入:arr = [6,2,7,5,1,2,0,3,10,2,5,0,5,5,0,8,7,6,8,0] + +输出:4.00000 +``` +示例 3: +``` +输入:arr = [6,0,7,0,7,5,7,8,3,4,0,7,8,1,6,8,1,1,2,4,8,1,9,5,4,3,8,5,10,8,6,6,1,0,6,10,8,2,3,4] + +输出:4.77778 +``` +示例 4: +``` +输入:arr = [9,7,8,7,7,8,4,4,6,8,8,7,6,8,8,9,2,6,0,0,1,10,8,6,3,3,5,1,10,9,0,7,10,0,10,4,1,10,6,9,3,6,0,0,2,7,0,6,7,2,9,7,7,3,0,1,6,1,10,3] + +输出:5.27778 +``` +示例 5: +``` +输入:arr = [4,8,4,10,0,7,1,3,7,8,8,3,4,1,6,2,1,1,8,0,9,8,0,3,9,10,3,10,1,10,7,3,2,1,4,9,10,7,6,4,0,8,5,1,2,1,6,2,5,0,7,10,9,10,3,7,10,5,8,5,7,6,7,6,10,9,5,10,5,5,7,2,10,7,7,8,2,0,1,1] + +输出:5.29167 +``` + +提示: +* $20 <= arr.length <= 1000$ +* `arr.length` 是 `20` 的 倍数  +* $0 <= arr[i] <= 10^5$ + +--- + +### 模拟 + +根据题意进行模拟即可:先对 `arr` 进行排序,再计算出待统计的左右端点(端点以外的数值为 `5%` 最值),最后计算出相应均值。 + +Java 代码: +```Java +class Solution { + public double trimMean(int[] arr) { + Arrays.sort(arr); + int n = arr.length, tot = 0; + for (int i = n / 20; i < n - n / 20; i++) tot += arr[i]; + return tot * 1.0 / (n * 0.9); + } +} +``` +TypeScript 代码: +```TypeScript +function trimMean(arr: number[]): number { + arr.sort((a,b)=>a-b) + let n = arr.length, tot = 0 + for (let i = n / 20; i < n - n / 20; i++) tot += arr[i] + return tot / (n * 0.9) +}; +``` +* 时间复杂度:$O(n\log{n})$ +* 空间复杂度:$O(\log{n})$ + +--- + +### 最后 + +这是我们「刷穿 LeetCode」系列文章的第 `No.1619` 篇,系列开始于 2021/01/01,截止于起始日 LeetCode 上共有 1916 道题目,部分是有锁题,我们将先把所有不带锁的题目刷完。 + +在这个系列文章里面,除了讲解解题思路以外,还会尽可能给出最为简洁的代码。如果涉及通解还会相应的代码模板。 + +为了方便各位同学能够电脑上进行调试和提交代码,我建立了相关的仓库:https://github.com/SharingSource/LogicStack-LeetCode 。 + +在仓库地址里,你可以看到系列文章的题解链接、系列文章的相应代码、LeetCode 原题链接和其他优选题解。 + diff --git "a/LeetCode/781-790/786. \347\254\254 K \344\270\252\346\234\200\345\260\217\347\232\204\347\264\240\346\225\260\345\210\206\346\225\260\357\274\210\345\233\260\351\232\276\357\274\211.md" "b/LeetCode/781-790/786. \347\254\254 K \344\270\252\346\234\200\345\260\217\347\232\204\347\264\240\346\225\260\345\210\206\346\225\260\357\274\210\345\233\260\351\232\276\357\274\211.md" index 38a12b77..0540807e 100644 --- "a/LeetCode/781-790/786. \347\254\254 K \344\270\252\346\234\200\345\260\217\347\232\204\347\264\240\346\225\260\345\210\206\346\225\260\357\274\210\345\233\260\351\232\276\357\274\211.md" +++ "b/LeetCode/781-790/786. \347\254\254 K \344\270\252\346\234\200\345\260\217\347\232\204\347\264\240\346\225\260\345\210\206\346\225\260\357\274\210\345\233\260\351\232\276\357\274\211.md" @@ -1,6 +1,6 @@ ### 题目描述 -这是 LeetCode 上的 **[786. 第 K 个最小的素数分数](https://leetcode-cn.com/problems/k-th-smallest-prime-fraction/solution/gong-shui-san-xie-yi-ti-shuang-jie-you-x-8ymk/)** ,难度为 **困难**。 +这是 LeetCode 上的 **[786. 第 K 个最小的素数分数](https://leetcode-cn.com/problems/k-th-smallest-prime-fraction/solution/gong-shui-san-xie-yi-ti-shuang-jie-you-x-8ymk/)** ,难度为 **中等**。 Tag : 「优先队列(堆)」、「多路归并」、「二分」、「双指针」 @@ -34,11 +34,11 @@ Tag : 「优先队列(堆)」、「多路归并」、「二分」、「双 提示: * $2 <= arr.length <= 1000$ -* $1 <= arr[i] <= 3 * 10^4$ -* $arr[0] == 1$ +* $1 <= arr[i] <= 3 \times 10^4$ +* $arr[0] = 1$ * $arr[i]$ 是一个 素数 ,$i > 0$ * $arr$ 中的所有数字 互不相同 ,且按**严格递增**排序 -* $1 <= k <= arr.length * (arr.length - 1) / 2$ +* $1 <= k <= arr.length \times (arr.length - 1) / 2$ --- @@ -74,7 +74,7 @@ class Solution { } } ``` -* 时间复杂度:扫描所有的点对复杂度为 $O(n^2)$;将二元组入堆和出堆的复杂度为 $O(\log{k})$。整体复杂度为 $O(n^2 * \log{k})$ +* 时间复杂度:扫描所有的点对复杂度为 $O(n^2)$;将二元组入堆和出堆的复杂度为 $O(\log{k})$。整体复杂度为 $O(n^2\log{k})$ * 空间复杂度:$O(k)$ --- @@ -115,7 +115,7 @@ class Solution { } } ``` -* 时间复杂度:起始将 $n - 1$ 个序列的头部元素放入堆中,复杂度为 $O(n\log{n})$;然后重复 $k$ 次操作得到第 $k$ 小的值,复杂度为 $O(k\log{n})$。整体复杂度为 $O(\max(n, k) * \log{n})$ +* 时间复杂度:起始将 $n - 1$ 个序列的头部元素放入堆中,复杂度为 $O(n\log{n})$;然后重复 $k$ 次操作得到第 $k$ 小的值,复杂度为 $O(k\log{n})$。整体复杂度为 $O(\max(n, k) \times \log{n})$ * 空间复杂度:$O(n)$ --- @@ -163,7 +163,7 @@ class Solution { } } ``` -* 时间复杂度:二分次数取决于精度,精度为 $C = 10^8$,二分复杂度为 $O(\log{C});$`check` 的复杂度为 $O(n)$。整体复杂度为 $O(n * \log{C})$ +* 时间复杂度:二分次数取决于精度,精度为 $C = 10^8$,二分复杂度为 $O(\log{C});$`check` 的复杂度为 $O(n)$。整体复杂度为 $O(n\log{C})$ * 空间复杂度:$O(1)$ ---