LeetCode·每日一题·1619.删除某些元素后的数组均值·排序·数学
2023-09-27 14:26:29 时间
链接:https://leetcode.cn/problems/mean-of-array-after-removing-some-elements/solution/-by-xun-ge-v-60u8/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
题目![](https://img-blog.csdnimg.cn/bae419ff2739473e9fad597d5ca6181e.png)
示例![](https://img-blog.csdnimg.cn/a1a73df86d944724b20a3ec67adae527.png)
思路
解题思路
其实就是一个数学题,先对数组元素进行排序,方便后面操作
对于 5% 转换为分数就是 1/20 ,对于最后 5% 转换为分数就是 19/20
因此直接遍历数组统计 1/20 到 19/20 之间的数据即可,最后取平均数
代码
static inline int cmp(const void *pa, const void *pb) {//排序
return *(int *)pa - *(int *)pb;
}
double trimMean(int* arr, int arrSize){
qsort(arr, arrSize, sizeof(int), cmp);//排序
double partialSum = 0;//统计累加和
for (int i = arrSize / 20; i < (19 * arrSize / 20); i++) {
partialSum += arr[i];
}
return partialSum / (arrSize * 0.9);//取平均值
}
作者:xun-ge-v
链接:https://leetcode.cn/problems/mean-of-array-after-removing-some-elements/solution/-by-xun-ge-v-60u8/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
相关文章
- [leetcode]Rotate List
- LeetCode_区间问题_中等_795.区间子数组个数
- LeetCode·每日一题·1832.判断句子是否为全字母句·模拟
- LeetCode·每日一题·1636.按照频率将数组升序排序·哈希
- LeetCode·每日一题·850.矩形面积 || ·【离散化+扫描线+数组】
- LeetCode·每日一题·593.有效的正方形·数学
- LeetCode·581.最短无序连续子数组·双指针
- LeetCode·每日一题·1331.数组序号转换·离散化
- LeetCode·301.删除无效的括号·BFS
- [LeetCode] 678. Valid Parenthesis String 验证括号字符串
- [LeetCode] 53. Maximum Subarray 最大子数组
- [LeetCode] 297. Serialize and Deserialize Binary Tree 二叉树的序列化和反序列化
- [LeetCode] 76. Minimum Window Substring 最小窗口子串
- leetcode 4 寻找两个正序数组的中位数
- leetcode 153 寻找选择排序数组中的最小值