首页 > 其他 > 详细

LeetCode 1093. Statistics from a Large Sample

时间:2019-08-24 14:23:33      阅读:89      评论:0      收藏:0      [点我收藏+]

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

 1 class Solution {
 2     public double[] sampleStats(int[] count) {
 3         double min = -1;
 4         double max = 0;
 5         int countSum = 0;
 6         double sum = 0;
 7         double maxCount = 0;
 8         double mode = 0;
 9         for(int i = 0; i<count.length; i++){
10             if(min==-1 && count[i]!=0){
11                 min = i;
12             }
13             
14             if(count[i] != 0){
15                 max = i;
16             }
17             
18             countSum += count[i];
19             sum += count[i]*i*1.0;
20             if(maxCount < count[i]){
21                 maxCount = count[i];
22                 mode = i;
23             }
24         }
25         
26         double median = 0.0;
27         int m1 = (countSum+1)/2;
28         int m2 = (countSum+2)/2;
29         int countNum = 0;
30         for(int i = 0; i<count.length; i++){
31             if(countNum<m1 && countNum+count[i]>=m1){
32                 median += i/2.0;
33             }
34             
35             if(countNum<m2 && countNum+count[i]>=m2){
36                 median += i/2.0;
37             }
38             
39             countNum+=count[i];
40         }
41         
42         return new double[]{min, max, sum/countSum, median, mode};
43     }
44 }

 

LeetCode 1093. Statistics from a Large Sample

原文:https://www.cnblogs.com/Dylan-Java-NYC/p/11404219.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!