{"id":6266,"date":"2020-02-06T21:38:00","date_gmt":"2020-02-07T05:38:00","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=6266"},"modified":"2020-02-06T21:38:16","modified_gmt":"2020-02-07T05:38:16","slug":"leetcode-1093-statistics-from-a-large-sample","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/algorithms\/array\/leetcode-1093-statistics-from-a-large-sample\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 1093. Statistics from a Large Sample"},"content":{"rendered":"\n<p>We sampled integers between&nbsp;<code>0<\/code>&nbsp;and&nbsp;<code>255<\/code>, and stored the results in an array&nbsp;<code>count<\/code>:&nbsp;&nbsp;<code>count[k]<\/code>&nbsp;is the number of integers we sampled equal to&nbsp;<code>k<\/code>.<\/p>\n\n\n\n<p>Return the minimum, maximum, mean, median, and mode of the sample respectively, as an array of&nbsp;<strong>floating point numbers<\/strong>.&nbsp; The mode is guaranteed to be unique.<\/p>\n\n\n\n<p><em>(Recall that the median of a sample is:<\/em><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><em>The middle element, if the elements of the sample were sorted and the number of elements is odd;<\/em><\/li><li><em>The average of the middle two elements, if the elements of the sample were sorted and the number of elements is even.)<\/em><\/li><\/ul>\n\n\n\n<p><strong>Example 1:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> 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]\n<strong>Output:<\/strong> [1.00000,3.00000,2.37500,2.50000,3.00000]\n<\/pre>\n\n\n\n<p><strong>Example 2:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> 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]\n<strong>Output:<\/strong> [1.00000,4.00000,2.18182,2.00000,1.00000]\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ol class=\"wp-block-list\"><li><code>count.length == 256<\/code><\/li><li><code>1 &lt;= sum(count) &lt;= 10^9<\/code><\/li><li>The mode of the sample that count represents is unique.<\/li><li>Answers within&nbsp;<code>10^-5<\/code>&nbsp;of the true value will be accepted as correct.<\/li><\/ol>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: TreeMap<\/strong><\/h2>\n\n\n\n<p>Time complexity: O(1)<br>Space complexity: O(1)<\/p>\n\n\n\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"C++\">\n\/\/ Author: Huahua\nclass Solution {\npublic:\n  vector<double> sampleStats(vector<int>& count) {\n    int counts = 0;\n    int minVal = 255;\n    int maxVal = 0;\n    int mode = -1;\n    int modeCount = 0;\n    long sum = 0;\n    map<int, int> m;\n    for (int i = 0; i <= 255; ++i) {      \n      if (!count[i]) continue;\n      sum += static_cast<long>(i) * count[i];\n      minVal = min(minVal, i);\n      maxVal = max(maxVal, i);\n      if (count[i] > modeCount) {\n        mode = i;\n        modeCount = count[i];\n      }\n      m[counts += count[i]] = i;\n    }\n    auto it1 = m.lower_bound(counts \/ 2);\n    double median = it1->second;    \n    auto it2 = next(it1);\n    if (counts % 2 == 0 && it2 != m.end() && it1->first == counts \/ 2) {  \n      median = (median + it2->second) \/ 2;\n    }\n    return {minVal, maxVal, static_cast<double>(sum) \/ counts, median, mode};\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>We sampled integers between&nbsp;0&nbsp;and&nbsp;255, and stored the results in an array&nbsp;count:&nbsp;&nbsp;count[k]&nbsp;is the number of integers we sampled equal to&nbsp;k. Return the minimum, maximum, mean, median,&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[184],"tags":[11,177,342,547],"class_list":["post-6266","post","type-post","status-publish","format-standard","hentry","category-array","tag-median","tag-medium","tag-mode","tag-statistics","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/6266","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/comments?post=6266"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/6266\/revisions"}],"predecessor-version":[{"id":6268,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/6266\/revisions\/6268"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=6266"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=6266"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=6266"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}