LeetCode 1093. Statistics from a Large Sample
原题链接在这里: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:
count.length == 2561 <= sum(count) <= 10^9- The mode of the sample that count represents is unique.
- Answers within
10^-5of 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的更多相关文章
- 【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 ...
- 【LeetCode】830. Positions of Large Groups 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- 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 ...
- LeetCode算法题-Positions of Large Groups(Java实现)
这是悦乐书的第323次更新,第346篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第193题(顺位题号是830).在由小写字母组成的字符串S中,那些相同的连续字符会组成集 ...
- 【leetcode】827. Making A Large Island
题目如下: 解题思路:这个题目可以进行拆分成几个子问题.第一,求出island的数量,其实就是 200. Number of Islands,这个很简单,DFS或者BFS都能搞定:第二,除了求出isl ...
- LeetCode 题解之 Positions of Large Groups
1.题目描述 2.问题分析 从头遍历字符串,使用一个局部迭代器和局部变量记录该字符个数.如果个数>= 3 ,则将此时的迭代器位置和局部迭代器的位置保存到局部vector中.再将这个局部vecto ...
- 算法与数据结构基础 - 双指针(Two Pointers)
双指针基础 双指针(Two Pointers)是面对数组.链表结构的一种处理技巧.这里“指针”是泛指,不但包括通常意义上的指针,还包括索引.迭代器等可用于遍历的游标. 同方向指针 设定两个指针.从头往 ...
- Sample Means(耶鲁大学教材)
Sample Means The sample mean from a group of observations is an estimate of the population mean. Giv ...
- Parametric Statistics
1.What are “Parametric Statistics”? 统计中的参数指的是总体的一个方面,而不是统计中的一个方面,后者指的是样本的一个方面.例如,总体均值是一个参数,而样本均值是一个统 ...
随机推荐
- 【实战经验】--Xilinx--IPcore--MCB(DDR3)运用
1.背景与介绍 1)在导师安排的新的任务中,用到了一块2G大小的DDR3(MT41K128M16JT-107).本打算像之前用SDRAM一样自己写初始化,读写模块,但是师兄跟我说可以用Xilinx自带 ...
- JAVA调用系统命令:python、shell等
实际项目开发场景中,可能会用到java项目调用系统命令的需求,如调用python或者shell脚本 可以参考如下例子,例子来源于ambari源码: \ambari\ambari-server\src\ ...
- .net Dapper 实践系列(4) ---数据查询(Layui+Ajax+Dapper+MySQL)
写在前面 上一小节,总结了数据显示时,会出现的日期问题.以及如何处理格式化日期.这个小节,主要总结的是使用Dapper 中的QueryMultiple方法依次显示查询多表的数据. 实践步骤 1.在Bo ...
- C# vb .net图像合成-多图片叠加合成
在.net中,如何简单快捷地实现图像合成呢,比如合成文字,合成艺术字,多张图片叠加合成等等?答案是调用SharpImage!专业图像特效滤镜和合成类库.下面开始演示关键代码,您也可以在文末下载全部源码 ...
- 自学Python编程的第九天(希望有大牛帮我看看我第一个代码是否有弊端,感谢您们)----------来自苦逼的转行人
2019-09-19-22:11:33 今天是自学Python的第九天 学的内容是有关文件操作的,如:r.w.a.rb.wb.ab.r+.w+.a+等 有大牛帮我看一下我的代码第一个有没有什么弊端吗? ...
- $.get、$.post、$getJSON、$ajax。
当我们用javascript写ajax程序写得很“开心”的时候,突然有人告诉你有一种东西叫jquery,它会告诉你不直接和HttpRequest是多么的快乐,同时你再也不需要再烦恼纠结的ajax乱码问 ...
- VUE回顾基础3
1.方法 在vue模板里函数被定义为方法来使用,将函数放在methods对象里,作为一个属性,就可以在模板里使用它 this:在方法中this指向该方法所属的组件,可以使用this方文档data对象的 ...
- v8--sort 方法 源码 (2) 快速排序法
v8 sort方法部分关于快速排序法的源码: function QuickSort(a, from, to) { // Insertion sort is faster for short array ...
- Leetcode刷题python
Two Sum 两数==target 方法二更好 题1,对时间复杂度有要求O(n),所以维护一个字典,遍历过的数值放在字典中,直接遍历时候查找字典中有没有出现差,查找字典时间复杂度是O(1),所以O( ...
- 学习操作系统和Linux内核的新体会
算起来是第三次看内核了吧,要从源码的细节中爬出来: (1)先拎清楚主要的数据结构,就把握住了骨架: (2)再看每个系统调用的功能的流程是如何围绕上述数据结构展开.举个栗子,块设备驱动层的主要数据结构有 ...