{"id":8238,"date":"2021-03-13T22:49:53","date_gmt":"2021-03-14T06:49:53","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=8238"},"modified":"2021-03-15T18:16:33","modified_gmt":"2021-03-16T01:16:33","slug":"leetcode-1792-maximum-average-pass-ratio","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/priority-queue\/leetcode-1792-maximum-average-pass-ratio\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 1792. Maximum Average Pass Ratio"},"content":{"rendered":"\n<figure class=\"wp-block-embed is-type-video is-provider-youtube wp-block-embed-youtube wp-embed-aspect-4-3 wp-has-aspect-ratio\"><div class=\"wp-block-embed__wrapper\">\n<iframe loading=\"lazy\" title=\"\u82b1\u82b1\u9171 LeetCode 1792. Maximum Average Pass Ratio - \u5237\u9898\u627e\u5de5\u4f5c EP389\" width=\"500\" height=\"281\" src=\"https:\/\/www.youtube.com\/embed\/wZMZemdsi90?feature=oembed\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share\" referrerpolicy=\"strict-origin-when-cross-origin\" allowfullscreen><\/iframe>\n<\/div><\/figure>\n\n\n\n<p>There is a school that has classes of students and each class will be having a final exam. You are given a 2D integer array&nbsp;<code>classes<\/code>, where&nbsp;<code>classes[i] = [pass<sub>i<\/sub>, total<sub>i<\/sub>]<\/code>. You know beforehand that in the&nbsp;<code>i<sup>th<\/sup><\/code>&nbsp;class, there are&nbsp;<code>total<sub>i<\/sub><\/code>&nbsp;total students, but only&nbsp;<code>pass<sub>i<\/sub><\/code>&nbsp;number of students will pass the exam.<\/p>\n\n\n\n<p>You are also given an integer&nbsp;<code>extraStudents<\/code>. There are another&nbsp;<code>extraStudents<\/code>&nbsp;brilliant students that are&nbsp;<strong>guaranteed<\/strong>&nbsp;to pass the exam of any class they are assigned to. You want to assign each of the&nbsp;<code>extraStudents<\/code>&nbsp;students to a class in a way that&nbsp;<strong>maximizes<\/strong>&nbsp;the&nbsp;<strong>average<\/strong>&nbsp;pass ratio across&nbsp;<strong>all<\/strong>&nbsp;the classes.<\/p>\n\n\n\n<p>The&nbsp;<strong>pass ratio<\/strong>&nbsp;of a class is equal to the number of students of the class that will pass the exam divided by the total number of students of the class. The&nbsp;<strong>average pass ratio<\/strong>&nbsp;is the sum of pass ratios of all the classes divided by the number of the classes.<\/p>\n\n\n\n<p>Return&nbsp;<em>the&nbsp;<strong>maximum<\/strong>&nbsp;possible average pass ratio after assigning the&nbsp;<\/em><code>extraStudents<\/code><em>&nbsp;students.&nbsp;<\/em>Answers within&nbsp;<code>10<sup>-5<\/sup><\/code>&nbsp;of the actual answer will be accepted.<\/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> classes = [[1,2],[3,5],[2,2]], <code>extraStudents<\/code> = 2\n<strong>Output:<\/strong> 0.78333\n<strong>Explanation:<\/strong> You can assign the two extra students to the first class. The average pass ratio will be equal to (3\/4 + 3\/5 + 2\/2) \/ 3 = 0.78333.\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> classes = [[2,4],[3,9],[4,5],[2,10]], <code>extraStudents<\/code> = 4\n<strong>Output:<\/strong> 0.53485\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>1 &lt;= classes.length &lt;= 10<sup>5<\/sup><\/code><\/li><li><code>classes[i].length == 2<\/code><\/li><li><code>1 &lt;= pass<sub>i<\/sub>&nbsp;&lt;= total<sub>i<\/sub>&nbsp;&lt;= 10<sup>5<\/sup><\/code><\/li><li><code>1 &lt;= extraStudents &lt;= 10<sup>5<\/sup><\/code><\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: Greedy + Heap<\/strong><\/h2>\n\n\n\n<figure class=\"wp-block-image size-large\"><a href=\"https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2021\/03\/1792-ep389-1.png\"><img loading=\"lazy\" decoding=\"async\" width=\"960\" height=\"540\" src=\"https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2021\/03\/1792-ep389-1.png\" alt=\"\" class=\"wp-image-8241\" srcset=\"https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2021\/03\/1792-ep389-1.png 960w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2021\/03\/1792-ep389-1-300x169.png 300w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2021\/03\/1792-ep389-1-768x432.png 768w\" sizes=\"auto, (max-width: 960px) 100vw, 960px\" \/><\/a><\/figure>\n\n\n\n<figure class=\"wp-block-image size-large\"><a href=\"https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2021\/03\/1792-ep389-2.png\"><img loading=\"lazy\" decoding=\"async\" width=\"960\" height=\"540\" src=\"https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2021\/03\/1792-ep389-2.png\" alt=\"\" class=\"wp-image-8242\" srcset=\"https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2021\/03\/1792-ep389-2.png 960w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2021\/03\/1792-ep389-2-300x169.png 300w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2021\/03\/1792-ep389-2-768x432.png 768w\" sizes=\"auto, (max-width: 960px) 100vw, 960px\" \/><\/a><\/figure>\n\n\n\n<p>Sort by the ratio increase potential (p + 1) \/ (t + 1) &#8211; p \/ t.<\/p>\n\n\n\n<p>Time complexity: O((m+n)logn)<br>Space complexity: O(n)<\/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  double maxAverageRatio(vector<vector<int>>& classes, int extraStudents) {\n    const int n = classes.size();\n    auto ratio = [&](int i, int delta = 0) {\n      return static_cast<double>(classes[i][0] + delta) \/ \n        (classes[i][1] + delta);\n    };\n    priority_queue<pair<double, int>> q;\n    for (int i = 0; i < n; ++i)\n      q.emplace(ratio(i, 1) - ratio(i), i);\n    while (extraStudents--) {\n      const auto [r, i] = q.top(); q.pop();      \n      ++classes[i][0];\n      ++classes[i][1];\n      q.emplace(ratio(i, 1) - ratio(i), i);\n    }\n    double total_ratio = 0;\n    for (int i = 0; i < n; ++i)\n      total_ratio += ratio(i);\n    return total_ratio \/ n;\n  }\n};\n<\/pre>\n\n<\/div><h2 class=\"tabtitle\">Python3<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"python\">\n# Author: Huahua\nclass Solution:\n  def maxAverageRatio(self, classes: List[List[int]], extraStudents: int) -> float:\n    def ratio(i, delta=0):\n      return (classes[i][0] + delta) \/ (classes[i][1] + delta)\n    q = []\n    for i, c in enumerate(classes):\n      heapq.heappush(q, (-(ratio(i, 1) - ratio(i)), i))\n    for _ in range(extraStudents):\n      _, i = heapq.heappop(q)\n      classes[i][0] += 1\n      classes[i][1] += 1\n      heapq.heappush(q, (-(ratio(i, 1) - ratio(i)), i))\n    return mean(ratio(i) for i, _ in enumerate(classes))\n<\/pre>\n<\/div><\/div>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>There is a school that has classes of students and each class will be having a final exam. You are given a 2D integer array&nbsp;classes,&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[674],"tags":[88,73,177,72],"class_list":["post-8238","post","type-post","status-publish","format-standard","hentry","category-priority-queue","tag-greedy","tag-heap","tag-medium","tag-priority-queue","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8238","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=8238"}],"version-history":[{"count":3,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8238\/revisions"}],"predecessor-version":[{"id":8244,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8238\/revisions\/8244"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=8238"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=8238"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=8238"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}