原题链接在这里:https://leetcode.com/problems/statistics-from-a-large-sample/

题目:

We sampled integers between 0 and 255, and stored the results in an array count:  count[k] is the number of integers we sampled equal to k.

Return the minimum, maximum, mean, median, and mode of the sample respectively, as an array of floating point numbers.  The mode is guaranteed to be unique.

(Recall that the median of a sample is:

  • The middle element, if the elements of the sample were sorted and the number of elements is odd;
  • The average of the middle two elements, if the elements of the sample were sorted and the number of elements is even.)

Example 1:

Input: count = [0,1,3,4,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]
Output: [1.00000,3.00000,2.37500,2.50000,3.00000]

Example 2:

Input: count = [0,4,3,2,2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]
Output: [1.00000,4.00000,2.18182,2.00000,1.00000] 

Constraints:

  1. count.length == 256
  2. 1 <= sum(count) <= 10^9
  3. The mode of the sample that count represents is unique.
  4. Answers within 10^-5 of the true value will be accepted as correct.

题解:

The count is the frequency of selected numbers.

mode is the number with highest frequency.

First iteration, calculate totoal number selected countSum.

If countSum is even, median should be between (countSum+1)/2 and (countSum+2)/2.

If countSum is odd, median should (countSum+1)/2. Here (countSum+2)/2 equals to (countSum+1)/2.

Inorder to make code consistent, it doesn't need to separate into odd and even cases, just take half from both indices.

Time Complexity: O(n). n = count.length. Two iterations.

Space: O(1).

AC Java:

 class Solution {
public double[] sampleStats(int[] count) {
double min = -1;
double max = 0;
int countSum = 0;
double sum = 0;
double maxCount = 0;
double mode = 0;
for(int i = 0; i<count.length; i++){
if(min==-1 && count[i]!=0){
min = i;
} if(count[i] != 0){
max = i;
} countSum += count[i];
sum += count[i]*i*1.0;
if(maxCount < count[i]){
maxCount = count[i];
mode = i;
}
} double median = 0.0;
int m1 = (countSum+1)/2;
int m2 = (countSum+2)/2;
int countNum = 0;
for(int i = 0; i<count.length; i++){
if(countNum<m1 && countNum+count[i]>=m1){
median += i/2.0;
} if(countNum<m2 && countNum+count[i]>=m2){
median += i/2.0;
} countNum+=count[i];
} return new double[]{min, max, sum/countSum, median, mode};
}
}

最新文章

  1. Spring获取ApplicationContext
  2. [ASP.NET MVC 大牛之路]03 - C#高级知识点概要(2) - 线程和并发
  3. ModernUI教程:MEF应用向导
  4. Xceed Ultimate Suite Xceed界面控件套包下载
  5. C#部分---函数添加基本格式;
  6. haproxy 规则匹配到了就停止,不会继续匹配下一个
  7. C语言里的文件函数
  8. bzoj 2281 [Sdoi2011]黑白棋(博弈+组合计数)
  9. 高放的python学习笔记之基本语法
  10. WPF-MVVM-ICommand接口实现
  11. 【PLM】【PDM】60页PPT终于说清了PDM和PLM的区别;智造时代,PLM系统10大应用趋势!
  12. Codeforces Round #545 Div. 1自闭记
  13. Django框架简介-模板系统
  14. QT QListWidget 简单的操作
  15. 使用@property - 廖雪峰的官方网站
  16. Python数据结构——栈的列表实现
  17. 【前端node.js框架】node.js框架express
  18. Dell 服务器安装方法介绍
  19. Detecting Underlying Linux Distro
  20. Session 共享(StateServer模式)(原创)

热门文章

  1. 手撕面试官系列(九):分布式限流面试专题 Nginx+zookeeper
  2. 全球DEM高程数据下载
  3. Python之路【第十一篇】:Python面向对象之封装
  4. 「UER#2」信息的交换
  5. 入门wpf—— 3、样式
  6. 2019 易车java面试笔试题 (含面试题解析)
  7. kali之HexorBase数据库破解
  8. vue-cli + webpack 环境搭建
  9. js继承(十)
  10. Unicode 字符和UTF编码的理解