zoukankan      html  css  js  c++  java
  • 630. Course Schedule III

    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/

    永远渴望,大智若愚(stay hungry, stay foolish)
  • 相关阅读:
    VS2010安装SP1后无法安装VS2010 SDK
    c#异常机制
    精简2003,安装后控制面板无效,点击控制面板就闪一下,返回桌面
    转载 Microsoft .NET Pet Shop 4 架构与技术分析
    JavaScript实用的一些技巧
    asp.net开发常用技巧收集
    Ajax+PHP+jQuery图片截图上传
    如何利用WINPE制作恢复光盘/恢复分区
    C/C++跨平台计时,精确到毫秒级别
    SetLocalInfo修改系统时间,立即生效
  • 原文地址:https://www.cnblogs.com/h-hkai/p/10225499.html
Copyright © 2011-2022 走看看