【leetcode】1093. Statistics from a Large Sample
题目如下:
We sampled integers between
0
and255
, and stored the results in an arraycount
:count[k]
is the number of integers we sampled equal tok
.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 == 256
1 <= sum(count) <= 10^9
- The mode of the sample that count represents is unique.
- Answers within
10^-5
of the true value will be accepted as correct.
解题思路:最大值,最小值,平均数和众数都很简单。中位数我是用两次循环求的,第一次求出样本的个数,根据个数求出中位数的下标,然后第二次循环即可得到。
代码如下:
class Solution(object):
def sampleStats(self, count):
"""
:type count: List[int]
:rtype: List[float]
"""
minv = None
maxv = 0
times = sum(count)
amount = 0
most = 0
mostCount = 0
inx1 = times / 2
if times % 2 == 0:
inx2 = inx1 - 1
else:
inx2 = None median1 = 0
median2 = None tmpTimes = 0
for i in range(len(count)):
if count[i] != 0 and minv == None:
minv = i
if count[i] != 0:
maxv = i
if count[i] != 0:
if count[i] > mostCount:
most = i
mostCount = count[i]
if inx1 >= tmpTimes and inx1 <= tmpTimes + count[i]:
median1 = i
if inx2 != None and inx2 >= tmpTimes and inx2 <= tmpTimes + count[i]:
median2 = i
tmpTimes += count[i] amount += (count[i]*i)
median = median1 if inx2 == None else (float(median2) + float(median1)) / float(2) return [float(minv),float(maxv),float(amount)/float(times),float(median),float(most)]
【leetcode】1093. Statistics from a Large Sample的更多相关文章
- LeetCode 1093. Statistics from a Large Sample
原题链接在这里:https://leetcode.com/problems/statistics-from-a-large-sample/ 题目: We sampled integers betwee ...
- 【LeetCode】Largest Number 解题报告
[LeetCode]Largest Number 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/largest-number/# ...
- 【LeetCode】386. Lexicographical Numbers 解题报告(Python)
[LeetCode]386. Lexicographical Numbers 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博 ...
- 【LeetCode】86. Partition List 解题报告(Python)
[LeetCode]86. Partition List 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http:// ...
- 【LeetCode】306. Additive Number 解题报告(Python)
[LeetCode]306. Additive Number 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http: ...
- 【LeetCode】817. Linked List Components 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- 【LeetCode】Minimum Depth of Binary Tree 二叉树的最小深度 java
[LeetCode]Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum dept ...
- 【Leetcode】Pascal's Triangle II
Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3, Return [1,3 ...
- 53. Maximum Subarray【leetcode】
53. Maximum Subarray[leetcode] Find the contiguous subarray within an array (containing at least one ...
随机推荐
- python开发环境准备
python 以版本众多,包之间依赖复杂而著称,所以一个趁手的开发环境还是很有必要的. 我的建议是用Anaconda做环境隔离.包管理,PyCharm做项目开发,jupyter做笔记,ipython和 ...
- ORACLE动态监听
动态监听的原理 pmon在数据库启动到mount或open时,动态从参数文件中读取service_names值.service_names可以为多值(可以有64个,其中包括两个系统的). servi ...
- First-order logic
w https://en.wikipedia.org/wiki/First-order_logic
- Linux_FTP服务器
目录 目录 FTP FTP Server FTP configuration Global config Anonymous user FTP Config Virtual user FTP Loca ...
- csr_matrix用法
1 csr_matrix默认对未填充的位置置为0, row = [0, 0, 0, 1, 1, 1, 2, 2, 2] # 行指标 col = [0, 1, 2, 0, 1, 2, 0, 1, 2] ...
- KETTLE——初见KETTLE
(PS:这是很早以前在CSDN上发过的,那个账号不想用了,所以搬过来) 就在前一段时间,因为公司需要突然被老大告知要用一个ETL工具,第一次知道这么个工具,完全不知道是做什么的.大概问了一下,说是一种 ...
- 44 答疑(三)--join的写法/Simple nested loop join的性能问题/Distinct和group by的性能/备库自增主键问题
44 答疑(三) Join的写法 35节介绍了join执行顺序,加了straight_join,两个问题: --1 如果用left join,左边的表一定是驱动表吗 --2 如果两个表的join包含多 ...
- Layerui 弹出层的位置设置
距顶 offset: '300px' 例1: layer.msg("请先选择项!", { offset: '300px' });例2: layer.confirm("确定 ...
- Queen Attack -- 微软2017年预科生计划在线编程笔试第二场
#!/usr/bin/env python # coding:utf-8 # Queen Attack # https://hihocoder.com/problemset/problem/1497 ...
- java webservice - cxf使用总结 一
1.创建maven项目 加入pom依赖 <dependency> <groupId>org.apache.cxf</groupId> <artifactId& ...