​LeetCode刷题实战630:课程表 III

算法的重要性,我就不多说了吧,想去大厂,就必须要经过基础知识和业务逻辑面试+算法面试。所以,为了提高大家的算法能力,这个公众号后续每天带大家做一道算法题,题目就从LeetCode上面选 !

今天和大家聊的问题叫做 课程表 III,我们先来看题面:

https://leetcode.cn/problems/course-schedule-iii/

There are n different online courses numbered from 1 to n. You are given an array courses where courses[i] = [durationi, lastDayi] indicate that the ith course should be taken continuously for durationi days and must be finished before or on lastDayi.

You will start on the 1st day and you cannot take two or more courses simultaneously.

Return the maximum number of courses that you can take.

这里有 n 门不同的在线课程,按从 1 到 n 编号。给你一个数组 courses ,其中 courses[i] = [durationi, lastDayi] 表示第 i 门课将会 持续 上 durationi 天课,并且必须在不晚于 lastDayi 的时候完成。

你的学期从第 1 天开始。且不能同时修读两门及两门以上的课程。

返回你最多可以修读的课程数目。

示例

示例 1:

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

示例 2:

输入:courses = [[1,2]]
输出:1

示例 3:

输入:courses = [[3,2],[4,3]]
输出:0

解题

https://blog.csdn.net/weixin_42051691/article/details/124830084

主要思路:

考虑两个因素,先考虑结束时间,其次考虑时间

优先选取最快要结束的课程(2 5 ,3 6)

如果当前课程时间冲突(完成时间大于结束时间),那么就从已经完成的课程中(包括当前课程)找一个需要时间最长的删除(当前课程优于时间长的课程)。

class Solution {
public:
// 优先学习最快要结束的课程,如果课程冲突,则将已经上过的持续时间最长的课程排除
    int scheduleCourse(vector<vector<int>>& courses) {
        priority_queue<int> pq;
        int n=courses.size();
        // 按照结束时间排序
        sort(courses.begin(),courses.end(),[](const vector<int>& l,const vector<int>& r){
            if(l[1]==r[1])
                return l[0]<r[0];
            return l[1]<r[1];
        });

        int sum=0;
        for(int i=0;i<n;i++){
            pq.push(courses[i][0]);
            sum+=courses[i][0];
            // 如果冲突,将前边时间最长的课程删去
            if(sum>courses[i][1])
                sum-=pq.top(),pq.pop();
        }
        return pq.size();
    }
};

上期推文:

LeetCode1-620题汇总,希望对你有点帮助!

LeetCode刷题实战621:任务调度器

LeetCode刷题实战622:设计循环队列

LeetCode刷题实战623:在二叉树中增加一行

LeetCode刷题实战624:数组列表中的最大距离

LeetCode刷题实战625:最小因式分解

LeetCode刷题实战626:换座位

LeetCode刷题实战627:变更性别

LeetCode刷题实战628:三个数的最大乘积

LeetCode刷题实战629:K个逆序对数组

3fbc1223857564172c68da55561d715b.png

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值