There are n different online courses numbered from 1 to n. Each course has some duration(course length) t and closed on dth day. A course should be taken continuously for t days and must be finished before or on the dth day. You will start at the 1st day.

Given n online courses represented by pairs (t,d), your task is to find the maximal number of courses that can be taken.

Example:

Input: [[100, 200], [200, 1300], [1000, 1250], [2000, 3200]]
Output: 3
Explanation:
There're totally 4 courses, but you can take 3 courses at most:
First, 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.
Second, 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.
Third, take the 2nd course, it costs 200 days so you will finish it on the 1300th day.
The 4th course cannot be taken now, since you will finish it on the 3300th day, which exceeds the closed date.

Note:

  1. The integer 1 <= d, t, n <= 10,000.
  2. You can't take two courses simultaneously.
 

Approach #1: C++. Using Recursion with memoization[memory Limit Exceeded]

class Solution {
public:
int scheduleCourse(vector<vector<int>>& courses) {
int m = courses.size();
if (m == 0) return 0; sort(courses.begin(), courses.end(), [](const auto& a, const auto& b) { return a[1] < b[1]; }); vector<vector<int>> memo(m, vector<int>(courses[m-1][1]+1, 0)); return schedule(courses, 0, 0, memo);
} int schedule(const vector<vector<int>>& courses, int i, int time, vector<vector<int>>& memo) {
if (i == courses.size()) return 0;
if (memo[i][time] != 0) return memo[i][time];
int taken = 0;
if (time + courses[i][0] <= courses[i][1])
taken = 1 + schedule(courses, i + 1, time + courses[i][0], memo);
int not_taken = schedule(courses, i + 1, time, memo);
memo[i][time] = max(taken, not_taken);
return memo[i][time];
}
};

  

Approach #2: Java.  Iterative Solution [Time Limit Exceeded]

class Solution {
public int scheduleCourse(int[][] courses) {
Arrays.sort(courses, (a, b) -> a[1] - b[1]);
int time = 0, count = 0;
for (int i = 0; i < courses.length; ++i) {
if (time + courses[i][0] <= courses[i][1]) {
time += courses[i][0];
count++;
} else {
int max_i = i;
for (int j = 0; j < i; ++j) {
if (courses[j][0] > courses[max_i][0])
max_i = j;
}
if (courses[max_i][0] > courses[i][0])
time += courses[i][0] - courses[max_i][0];
courses[max_i][0] = -1;
}
}
return count;
}
}

  

Approach #3 C++. [priority_queue]

class Solution {
public:
int scheduleCourse(vector<vector<int>>& courses) {
int m = courses.size();
if (m == 0) return 0; sort(courses.begin(), courses.end(), [](const auto& a, const auto& b) { return a[1] < b[1]; }); priority_queue<int> pq;
int time = 0, count = 0; for (auto course : courses) {
if (time + course[0] <= course[1]) {
count++;
time += course[0];
pq.push(course[0]);
} else {
if (!pq.empty() && pq.top() > course[0]) {
time += course[0] - pq.top();
//cout << pq.top() << endl;
pq.pop();
pq.push(course[0]);
}
}
} return count;
}
};

  

Analysis:

First: sorting the courses with the end time form little to big.

Second: we use a variable time to mark up the current time. If time + course[0] <= course[1], we update the ans and time. otherwise, we find out the max duration in the pass courses we have taken. (we can use priority_queue to maintain the max duration)

Thrid: we add the course[0] to the time and push it to priority_queue then we subtract the max duration from time and pop it from the priority_queue.

reference:

https://leetcode.com/problems/course-schedule-iii/solution/

630. Course Schedule III的更多相关文章

  1. [LeetCode] Course Schedule III 课程清单之三

    There are n different online courses numbered from 1 to n. Each course has some duration(course leng ...

  2. [leetcode-630-Course Schedule III]

    There are n different online courses numbered from 1 to n. Each course has some duration(course leng ...

  3. [Swift]LeetCode630. 课程表 III | Course Schedule III

    There are n different online courses numbered from 1 to n. Each course has some duration(course leng ...

  4. 贪心-Course Schedule III

    2020-02-01 21:37:39 问题描述: 问题求解: 对于课程来说截止时间在前面的肯定需要优先安排,所以首先需要将courses按照deadline进行排序. 然后只需要不断的加入当前的课程 ...

  5. LeetCode All in One题解汇总(持续更新中...)

    突然很想刷刷题,LeetCode是一个不错的选择,忽略了输入输出,更好的突出了算法,省去了不少时间. dalao们发现了任何错误,或是代码无法通过,或是有更好的解法,或是有任何疑问和建议的话,可以在对 ...

  6. 算法与数据结构基础 - 贪心(Greedy)

    贪心基础 贪心(Greedy)常用于解决最优问题,以期通过某种策略获得一系列局部最优解.从而求得整体最优解. 贪心从局部最优角度考虑,只适用于具备无后效性的问题,即某个状态以前的过程不影响以后的状态. ...

  7. All LeetCode Questions List 题目汇总

    All LeetCode Questions List(Part of Answers, still updating) 题目汇总及部分答案(持续更新中) Leetcode problems clas ...

  8. leetcode 学习心得 (3)

    源代码地址:https://github.com/hopebo/hopelee 语言:C++ 517. Super Washing Machines You have n super washing ...

  9. leetcode hard

    # Title Solution Acceptance Difficulty Frequency     4 Median of Two Sorted Arrays       27.2% Hard ...

随机推荐

  1. Cassandra学习五 使用Key的正确姿势

    NoSQL一般是反范式的,比如提倡数据冗余,使得不至于写出非常复杂的SQL语句. Cassandra之中一共包含下面5中Key: Primary Key: 用来获取某一行的数据,可以是一列或多列   ...

  2. python学习(五) 条件、循环和其他语句

    第五章 条件.循环和其他语句 5.1 print和import的更多信息 5.1.1 使用逗号输出 >>> print('age',43,45)         // 可以用逗号隔开 ...

  3. SecureCRT乱码问题解决方法

    环境:SecureCRT登陆REDHAT5.3 LINUX系统 问题:vi编辑器编辑文件时文件中的内容中文显示乱码,但是直接使用linux系统terminal打开此文件时中文显示正常,确诊问题出现在客 ...

  4. 使用原生js自定义内置标签

    使用原生js自定义内置标签 效果图 代码 <!DOCTYPE html> <html lang="en"> <head> <meta ch ...

  5. java成神之——Stream和Optional

    Stream流 基本使用 流关闭 平行流 流重用 iterator转换成流 分组计数 无限流 流转集合 压缩流 统计数值流 集合转换流遍历 流拼接 reduce 使用流生成随机字符串 流的包装流 几种 ...

  6. ACCESS中如何比较日期和时间,使用DateDiff函数

    DateDiff,语法如下:DateDiff( 间隔字符, 日期1, 日期2 [,firstdayofweek[, firstweekofyear]])一般使用 DateDiff( 间隔字符, 日期1 ...

  7. Easyui Datagrid 如何实现后台交互显示用户数据列表

    转自:https://blog.csdn.net/Tomsheng321/article/details/50722571?utm_source=blogxgwz9 新手初学的时候可能有个疑问:如何在 ...

  8. django表单的字段验证(clean_<fieldname>())和ajax的字段验证

    django中的Form有个很重要的功能:验证用户输入 而验证用户输入也可以分为2种: (1)前端本身的验证,例如:字段是否可为空,手机号码格式是否正确等: (2)前端输入数据和后台数据库数据的验证, ...

  9. ubuntu主目录下的中文文件夹名改回英文

    linux下经常用命令行,目录有中文输起来非常麻烦,想把他改回英文于是登录的时候选择英文发现没装英文语言环境,为这个重新装麻烦,只能再想办法 找了一下发现传话里有个用户文件夹更新,命令是xdg-use ...

  10. Properties & Method

    [Properties] 1.lazy property,通过@property来定义, lazy property的属性直到使用的时候才初始化: 2.Computed Properties: 2.对 ...