{"id":8598,"date":"2021-10-04T20:22:05","date_gmt":"2021-10-05T03:22:05","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=8598"},"modified":"2021-10-04T20:28:16","modified_gmt":"2021-10-05T03:28:16","slug":"leetcode-2028-find-missing-observations","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/math\/leetcode-2028-find-missing-observations\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 2028. Find Missing Observations"},"content":{"rendered":"\n<p>You have observations of&nbsp;<code>n + m<\/code>&nbsp;<strong>6-sided<\/strong>&nbsp;dice rolls with each face numbered from&nbsp;<code>1<\/code>&nbsp;to&nbsp;<code>6<\/code>.&nbsp;<code>n<\/code>&nbsp;of the observations went missing, and you only have the observations of&nbsp;<code>m<\/code>&nbsp;rolls. Fortunately, you have also calculated the&nbsp;<strong>average value<\/strong>&nbsp;of the&nbsp;<code>n + m<\/code>&nbsp;rolls.<\/p>\n\n\n\n<p>You are given an integer array&nbsp;<code>rolls<\/code>&nbsp;of length&nbsp;<code>m<\/code>&nbsp;where&nbsp;<code>rolls[i]<\/code>&nbsp;is the value of the&nbsp;<code>i<sup>th<\/sup><\/code>&nbsp;observation. You are also given the two integers&nbsp;<code>mean<\/code>&nbsp;and&nbsp;<code>n<\/code>.<\/p>\n\n\n\n<p>Return&nbsp;<em>an array of length&nbsp;<\/em><code>n<\/code><em>&nbsp;containing the missing observations such that the&nbsp;<strong>average value&nbsp;<\/strong>of the&nbsp;<\/em><code>n + m<\/code><em>&nbsp;rolls is&nbsp;<strong>exactly<\/strong>&nbsp;<\/em><code>mean<\/code>. If there are multiple valid answers, return&nbsp;<em>any of them<\/em>. If no such array exists, return&nbsp;<em>an empty array<\/em>.<\/p>\n\n\n\n<p>The&nbsp;<strong>average value<\/strong>&nbsp;of a set of&nbsp;<code>k<\/code>&nbsp;numbers is the sum of the numbers divided by&nbsp;<code>k<\/code>.<\/p>\n\n\n\n<p>Note that&nbsp;<code>mean<\/code>&nbsp;is an integer, so the sum of the&nbsp;<code>n + m<\/code>&nbsp;rolls should be divisible by&nbsp;<code>n + m<\/code>.<\/p>\n\n\n\n<p><strong>Example 1:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> rolls = [3,2,4,3], mean = 4, n = 2\n<strong>Output:<\/strong> [6,6]\n<strong>Explanation:<\/strong> The mean of all n + m rolls is (3 + 2 + 4 + 3 + 6 + 6) \/ 6 = 4.\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> rolls = [1,5,6], mean = 3, n = 4\n<strong>Output:<\/strong> [2,3,2,2]\n<strong>Explanation:<\/strong> The mean of all n + m rolls is (1 + 5 + 6 + 2 + 3 + 2 + 2) \/ 7 = 3.\n<\/pre>\n\n\n\n<p><strong>Example 3:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> rolls = [1,2,3,4], mean = 6, n = 4\n<strong>Output:<\/strong> []\n<strong>Explanation:<\/strong> It is impossible for the mean to be 6 no matter what the 4 missing rolls are.\n<\/pre>\n\n\n\n<p><strong>Example 4:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> rolls = [1], mean = 3, n = 1\n<strong>Output:<\/strong> [5]\n<strong>Explanation:<\/strong> The mean of all n + m rolls is (1 + 5) \/ 2 = 3.\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>m == rolls.length<\/code><\/li><li><code>1 &lt;= n, m &lt;= 10<sup>5<\/sup><\/code><\/li><li><code>1 &lt;= rolls[i], mean &lt;= 6<\/code><\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: Math &amp; Greedy<\/strong><\/h2>\n\n\n\n<p>Total sum = (m + n) * mean<br>Left = Total sum &#8211; sum(rolls) = (m + n) * mean &#8211; sum(rolls)<br>If left &gt; 6 * n or left &lt; 1 * n, then there is no solution.<br>Otherwise, we need to distribute Left into n rolls.<br>There are very ways to do that, one of them is even distribution, e.g. using the average number as much as possible, and use avg + 1 to fill the gap.<br>Compute the average and reminder: x = left \/ n, r = left % n.<br>there will be n &#8211; r of x and r of x + 1 in the output array.<\/p>\n\n\n\n<p>e.g. [1, 5, 6], mean = 3, n = 4<br>Total sum = (3 + 4) * 3 = 21<br>Left = 21 &#8211; (1 + 5 + 6) = 9<br>x = 9 \/ 4 = 2, r = 9 % 4 = 1<br>Ans = [2, 2, 2, 2+1] = [2,2,2,3]<\/p>\n\n\n\n<p>Time complexity: O(m + n)<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++\">\nclass Solution {\npublic:\n  vector<int> missingRolls(vector<int>& rolls, int mean, int n) {\n    const int m = rolls.size();    \n    int t = accumulate(begin(rolls), end(rolls), 0);\n    int left = mean * (m + n) - t;\n    if (left > 6 * n || left < n) return {};    \n    vector<int> ans(n, left \/ n);\n    for (int i = 0; i < left % n; ++i) ++ans[i];\n    return ans;\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>You have observations of&nbsp;n + m&nbsp;6-sided&nbsp;dice rolls with each face numbered from&nbsp;1&nbsp;to&nbsp;6.&nbsp;n&nbsp;of the observations went missing, and you only have the observations of&nbsp;m&nbsp;rolls. Fortunately, you&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[49],"tags":[31],"class_list":["post-8598","post","type-post","status-publish","format-standard","hentry","category-math","tag-math","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8598","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=8598"}],"version-history":[{"count":3,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8598\/revisions"}],"predecessor-version":[{"id":8602,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8598\/revisions\/8602"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=8598"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=8598"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=8598"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}