<kbd id="afajh"><form id="afajh"></form></kbd>
<strong id="afajh"><dl id="afajh"></dl></strong>
    <del id="afajh"><form id="afajh"></form></del>
        1. <th id="afajh"><progress id="afajh"></progress></th>
          <b id="afajh"><abbr id="afajh"></abbr></b>
          <th id="afajh"><progress id="afajh"></progress></th>

          ?LeetCode刷題實(shí)戰(zhàn)630:課程表 III

          共 1584字,需瀏覽 4分鐘

           ·

          2022-06-09 01:01

          算法的重要性,我就不多說了吧,想去大廠,就必須要經(jīng)過基礎(chǔ)知識(shí)和業(yè)務(wù)邏輯面試+算法面試。所以,為了提高大家的算法能力,這個(gè)公眾號(hào)后續(xù)每天帶大家做一道算法題,題目就從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 編號(hào)。給你一個(gè)數(shù)組 courses ,其中 courses[i] = [durationi, lastDayi] 表示第 i 門課將會(huì) 持續(xù) 上 durationi 天課,并且必須在不晚于 lastDayi 的時(shí)候完成。


          你的學(xué)期從第 1 天開始。且不能同時(shí)修讀兩門及兩門以上的課程。


          返回你最多可以修讀的課程數(shù)目。


          示例

          示例 1:

          輸入:courses = [[100, 200], [200, 1300], [1000, 1250], [2000, 3200]]
          輸出:3
          解釋:
          這里一共有 4 門課程,但是你最多可以修 3 門:
          首先,修第 1 門課,耗費(fèi) 100 天,在第 100 天完成,在第 101 天開始下門課。
          第二,修第 3 門課,耗費(fèi) 1000 天,在第 1100 天完成,在第 1101 天開始下門課程。
          第三,修第 2 門課,耗時(shí) 200 天,在第 1300 天完成。
          第 4 門課現(xiàn)在不能修,因?yàn)閷?huì)在第 3300 天完成它,這已經(jīng)超出了關(guān)閉日期。

          示例 2:

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

          示例 3:

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


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


          主要思路:

          考慮兩個(gè)因素,先考慮結(jié)束時(shí)間,其次考慮時(shí)間

          優(yōu)先選取最快要結(jié)束的課程(2 5 ,3 6)

          如果當(dāng)前課程時(shí)間沖突(完成時(shí)間大于結(jié)束時(shí)間),那么就從已經(jīng)完成的課程中(包括當(dāng)前課程)找一個(gè)需要時(shí)間最長的刪除(當(dāng)前課程優(yōu)于時(shí)間長的課程)。


          class?Solution?{
          public:
          // 優(yōu)先學(xué)習(xí)最快要結(jié)束的課程,如果課程沖突,則將已經(jīng)上過的持續(xù)時(shí)間最長的課程排除
          ????int?scheduleCourse(vector<vector<int>>& courses)?{
          ????????priority_queue<int> pq;
          ????????int?n=courses.size();
          ????????// 按照結(jié)束時(shí)間排序
          ????????sort(courses.begin(),courses.end(),[](const?vector<int>& l,const?vector<int>& r){
          ????????????if(l[1]==r[1])
          ????????????????return?l[0]0];
          ????????????return?l[1]1];
          ????????});

          ????????int?sum=0;
          ????????for(int?i=0;i????????????pq.push(courses[i][0]);
          ????????????sum+=courses[i][0];
          ????????????// 如果沖突,將前邊時(shí)間最長的課程刪去
          ????????????if(sum>courses[i][1])
          ????????????????sum-=pq.top(),pq.pop();
          ????????}
          ????????return?pq.size();
          ????}
          };

          上期推文:
          LeetCode1-620題匯總,希望對(duì)你有點(diǎn)幫助!
          LeetCode刷題實(shí)戰(zhàn)621:任務(wù)調(diào)度器
          LeetCode刷題實(shí)戰(zhàn)622:設(shè)計(jì)循環(huán)隊(duì)列
          LeetCode刷題實(shí)戰(zhàn)623:在二叉樹中增加一行
          LeetCode刷題實(shí)戰(zhàn)624:數(shù)組列表中的最大距離
          LeetCode刷題實(shí)戰(zhàn)625:最小因式分解
          LeetCode刷題實(shí)戰(zhàn)626:換座位
          LeetCode刷題實(shí)戰(zhàn)627:變更性別
          LeetCode刷題實(shí)戰(zhàn)628:三個(gè)數(shù)的最大乘積
          LeetCode刷題實(shí)戰(zhàn)629:K個(gè)逆序?qū)?shù)組

          瀏覽 22
          點(diǎn)贊
          評(píng)論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報(bào)
          評(píng)論
          圖片
          表情
          推薦
          點(diǎn)贊
          評(píng)論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報(bào)
          <kbd id="afajh"><form id="afajh"></form></kbd>
          <strong id="afajh"><dl id="afajh"></dl></strong>
            <del id="afajh"><form id="afajh"></form></del>
                1. <th id="afajh"><progress id="afajh"></progress></th>
                  <b id="afajh"><abbr id="afajh"></abbr></b>
                  <th id="afajh"><progress id="afajh"></progress></th>
                  AV天堂无码 | 亚洲一区二区三区蜜桃 | 插BB免费看 | 网站自拍视频网站在线看 | a免费在线观看 |