leetcode 630 课程表

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.

这里有 n 门不同的在线课程,他们按从 1 到 n 编号。每一门课程有一定的持续上课时间(课程时间)t 以及关闭时间第 d 天。一门课要持续学习 t 天直到第 d 天时要完成,你将会从第 1 天开始。

给出 n 个在线课程用 (t, d) 对表示。你的任务是找出最多可以修几门课。

 

示例:

输入: [[100, 200], [200, 1300], [1000, 1250], [2000, 3200]]
输出: 3
解释: 
这里一共有 4 门课程, 但是你最多可以修 3 门:
首先, 修第一门课时, 它要耗费 100 天,你会在第 100 天完成, 在第 101 天准备下门课。
第二, 修第三门课时, 它会耗费 1000 天,所以你将在第 1100 天的时候完成它, 以及在第 1101 天开始准备下门课程。
第三, 修第二门课时, 它会耗时 200 天,所以你将会在第 1300 天时完成它。
第四门课现在不能修,因为你将会在第 3300 天完成它,这已经超出了关闭日期。

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/course-schedule-iii
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

 

解题思路:

      看完题目首先的思路是先考虑截止日期早的课程,所以调用sort函数将courses向量重排一遍。

      遍历向量中的所有课程,如果已花时间加上课程时间大于当前课程的截止时间,那就将已选课程中最大的课程持续时间与当前课程持续时间比较,选择较小的那门课程,这样所选课程总数不变,但是所花时间更少。所以需要维持一个已选课程的堆。

class Solution {
public:
    int scheduleCourse(vector<vector<int>>& courses) 
    {
        if(courses.size() == 0) return 0 ;
        sort(courses.begin() , courses.end() , [](const auto a , const auto b)
             {
                 return a[1] < b[1] ;
             });
        
        priority_queue<int> cour ; //参加的课程
        int time = 0 ; //所花的全部时间
        
        for(const auto cor : courses)
        {
            if(time + cor[0] <= cor[1])
            {
                time += cor[0] ;
                cour.push(cor[0]) ;
            }
            else if(!cour.empty() && cour.top() > cor[0])
            {
                time += cor[0] - cour.top() ; 
                cour.pop() ;
                cour.push(cor[0]) ;
            }
        }
        
        return cour.size() ;
    }
};

向算法传递函数,可以传递函数,函数指针,lambda表达式以及函数对象。这些都属于可调用对象,可以对其使用函数调用运算符。

sort(courses.begin() , courses.end() , [](const auto a , const auto b)
             {
                 return a[1] < b[1] ;
             });

这个使用的是lambda表达式。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值