原题链接在这里: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};
}
}

LeetCode 1093. Statistics from a Large Sample的更多相关文章

  1. 【leetcode】1093. Statistics from a Large Sample

    题目如下: We sampled integers between 0 and 255, and stored the results in an array count:  count[k] is ...

  2. 【LeetCode】830. Positions of Large Groups 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

  3. Baozi Leetcode solution 1036: Escape a Large Maze

    Problem Statement In a 1 million by 1 million grid, the coordinates of each grid square are (x, y) w ...

  4. LeetCode算法题-Positions of Large Groups(Java实现)

    这是悦乐书的第323次更新,第346篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第193题(顺位题号是830).在由小写字母组成的字符串S中,那些相同的连续字符会组成集 ...

  5. 【leetcode】827. Making A Large Island

    题目如下: 解题思路:这个题目可以进行拆分成几个子问题.第一,求出island的数量,其实就是 200. Number of Islands,这个很简单,DFS或者BFS都能搞定:第二,除了求出isl ...

  6. LeetCode 题解之 Positions of Large Groups

    1.题目描述 2.问题分析 从头遍历字符串,使用一个局部迭代器和局部变量记录该字符个数.如果个数>= 3 ,则将此时的迭代器位置和局部迭代器的位置保存到局部vector中.再将这个局部vecto ...

  7. 算法与数据结构基础 - 双指针(Two Pointers)

    双指针基础 双指针(Two Pointers)是面对数组.链表结构的一种处理技巧.这里“指针”是泛指,不但包括通常意义上的指针,还包括索引.迭代器等可用于遍历的游标. 同方向指针 设定两个指针.从头往 ...

  8. Sample Means(耶鲁大学教材)

    Sample Means The sample mean from a group of observations is an estimate of the population mean. Giv ...

  9. Parametric Statistics

    1.What are “Parametric Statistics”? 统计中的参数指的是总体的一个方面,而不是统计中的一个方面,后者指的是样本的一个方面.例如,总体均值是一个参数,而样本均值是一个统 ...

随机推荐

  1. LuoguP2698 【[USACO12MAR]花盆Flowerpot】

    题目描述 首先我们简化一下题意: 要找一段区间[L,R],使区间[L,R]内元素最大值减最小值大于等于D. 做法: 首先很容易想到采用二分,分什么呢? 我们二分区间长度为mid 这个时候,检验就成为了 ...

  2. 洛谷 P1411 树 (树形dp)

    大意: 给定树, 求删除一些边, 使得连通块大小的乘积最大 设$dp_{i,j}$表示只考虑点$i$的子树, $i$所在连通块大小为$j$的最大值. 转移的时候不计算$i$所在连通块的贡献, 留到最后 ...

  3. gin-swagger生成API文档

    github地址:https://github.com/swaggo/gin-swagger 下载安装cmd/swag命令工具包 先下载cmd包,才能执行相关命令 go get -u github.c ...

  4. Python进阶----线程基础,开启线程的方式(类和函数),线程VS进程,线程的方法,守护线程,详解互斥锁,递归锁,信号量

    Python进阶----线程基础,开启线程的方式(类和函数),线程VS进程,线程的方法,守护线程,详解互斥锁,递归锁,信号量 一丶线程的理论知识 什么是线程:    1.线程是一堆指令,是操作系统调度 ...

  5. k8s--yml文件2

  6. python全栈学习路线

    python全栈学习路线-查询笔记 查询目录 一,硬件                                                                    十一,数据 ...

  7. Java GC的工作原理详解

    JVM学习笔记之JVM内存管理和JVM垃圾回收的概念,JVM内存结构由堆.栈.本地方法栈.方法区等部分组成,另外JVM分别对新生代下载地址  和旧生代采用不同的垃圾回收机制. 首先来看一下JVM内存结 ...

  8. Python人工智能第二篇:人脸检测和图像识别

    Python人工智能第二篇:人脸检测和图像识别 人脸检测 详细内容请看技术文档:https://ai.baidu.com/docs#/Face-Python-SDK/top from aip impo ...

  9. Nginx 常用命令并实现最基本的反向代理

    nginx 命令 测试配置文件格式是否正确:$ nginx -t 启动:nginx 重启:nginx -s reload 获取nginx进程号: ps -ef|grep nginx 停止进程(mast ...

  10. Centos7搭建Harbor私有仓库(二)

    1 说明 前文Centos7搭建Harbor私有仓库(一)中成功搭建了Harbor,但,是以http方式搭建的,这里我们修改为https方式 以下基于镜像CentOS-7-x86_64-Minimal ...