{"id":5171,"date":"2019-05-10T22:52:25","date_gmt":"2019-05-11T05:52:25","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=5171"},"modified":"2019-05-10T22:55:48","modified_gmt":"2019-05-11T05:55:48","slug":"leetcode-630-course-schedule-iii","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/greedy\/leetcode-630-course-schedule-iii\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 630. Course Schedule III"},"content":{"rendered":"\n<p>There are&nbsp;<code>n<\/code>&nbsp;different online courses numbered from&nbsp;<code>1<\/code>&nbsp;to&nbsp;<code>n<\/code>. Each course has some duration(course length)&nbsp;<code>t<\/code>&nbsp;and closed on&nbsp;<code>d<sub>th<\/sub><\/code>&nbsp;day. A course should be taken&nbsp;<strong>continuously<\/strong>for&nbsp;<code>t<\/code>&nbsp;days and must be finished before or on the&nbsp;<code>d<sub>th<\/sub><\/code>&nbsp;day. You will start at the&nbsp;<code>1<sub>st<\/sub><\/code>&nbsp;day.<\/p>\n\n\n\n<p>Given&nbsp;<code>n<\/code>&nbsp;online courses represented by pairs&nbsp;<code>(t,d)<\/code>, your task is to find the maximal number of courses that can be taken.<\/p>\n\n\n\n<p><strong>Example:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted; crayon:false\"><strong>Input:<\/strong> [[100, 200], [200, 1300], [1000, 1250], [2000, 3200]]\n<strong>Output:<\/strong> 3\n<strong>Explanation:<\/strong> \nThere're totally 4 courses, but you can take 3 courses at most:\nFirst, take the 1st course, it costs 100 days so you will finish it on the 100th day, and ready to take the next course on the 101st day.\nSecond, take the 3rd course, it costs 1000 days so you will finish it on the 1100th day, and ready to take the next course on the 1101st day. \nThird, take the 2nd course, it costs 200 days so you will finish it on the 1300th day. \nThe 4th course cannot be taken now, since you will finish it on the 3300th day, which exceeds the closed date.\n<\/pre>\n\n\n\n<p><strong>Note:<\/strong><\/p>\n\n\n\n<ol class=\"wp-block-list\"><li>The integer 1 &lt;= d, t, n &lt;= 10,000.<\/li><li>You can&#8217;t take two courses simultaneously.<\/li><\/ol>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: Priority queue<\/strong><\/h2>\n\n\n\n<ol class=\"wp-block-list\"><li>Sort courses by end date<\/li><li>Use a priority queue (Max-Heap) to store the course lengths or far<\/li><li>Swap with a longer course if we could not take the current one<\/li><\/ol>\n\n\n\n<p>Time complexity: O(nlogn)<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, running time: 196 ms, 40.5 MB\nclass Solution {\npublic:\n  int scheduleCourse(vector<vector<int>>& courses) {\n    sort(begin(courses), end(courses), [](const auto& a, const auto& b) {\n      return a[1] < b[1];\n    });\n    priority_queue<int> q;\n    int d = 0;\n    for (const auto& c: courses) {\n      if (d + c[0] <= c[1]) {        \n        d += c[0];\n        q.push(c[0]);\n      } else if (q.size() &#038;&#038; q.top() > c[0]) {\n        d += c[0] - q.top(); q.pop();\n        q.push(c[0]);\n      }\n    }\n    return q.size();\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>There are&nbsp;n&nbsp;different online courses numbered from&nbsp;1&nbsp;to&nbsp;n. Each course has some duration(course length)&nbsp;t&nbsp;and closed on&nbsp;dth&nbsp;day. A course should be taken&nbsp;continuouslyfor&nbsp;t&nbsp;days and must be finished before or&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[51],"tags":[88,217,72],"class_list":["post-5171","post","type-post","status-publish","format-standard","hentry","category-greedy","tag-greedy","tag-hard","tag-priority-queue","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/5171","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=5171"}],"version-history":[{"count":3,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/5171\/revisions"}],"predecessor-version":[{"id":5174,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/5171\/revisions\/5174"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=5171"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=5171"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=5171"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}