Offer to come, dig friends take it! I am participating in the 2022 Spring Recruit Punch card activity. Click here for details
describe
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.
Example 1:
Input: courses = [[100,200],[200,1300],[1000,1250],[2000,3200]]
Output: 3
Explanation:
There are 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.
Copy the code
Note:
- 1 <= courses.length <= 10^4
- 1 <= durationi, lastDayi< = 10 ^ 4
parsing
There are n different online courses, numbered from 1 to N, depending on the topic. Given an array of courses where courses[I] = [durationi, lastDayi] indicates that the I course should be studied consecutively on durationi days and must be completed before lastDayi ends. Start classes on day 1, but cannot take more than two classes at the same time. Return the maximum number of classes you can take.
We can start with the courses that take the least time before a certain deadline and try to learn as many courses as possible. If we can finish the course before the deadline, then the course will become N+1. If not, we will remove the course that takes the longest time in the current N+1 course. Currently, we still only have N courses. However, we minimize and optimize the time already spent, so that we can have more time to learn new courses later.
answer
from sortedcontainers import SortedList
class Solution(object):
def scheduleCourse(self, courses):
"""
:type courses: List[List[int]]
:rtype: int
"""
courses.sort(key=lambda x:x[1])
L = SortedList([])
days = 0
for i in range(len(courses)):
L.add(courses[i][0])
days += courses[i][0]
if days>courses[i][1]:
days -= L.pop()
return len(L)
Copy the code
The results
Given the linked list in the Python online submission. Memory Usage: 10000 ms Submissions in the Python online submissions for Course Schedule III.Copy the code
parsing
You can do the same thing with the big top heap, which is a little bit faster.
answer
class Solution(object):
def scheduleCourse(self, courses):
"""
:type courses: List[List[int]]
:rtype: int
"""
heap = []
days = 0
for dur, last in sorted(courses, key=lambda x:x[1]):
if dur + days <= last:
days += dur
heappush(heap, -dur)
elif heap and -heap[0]>dur:
days += dur+heappop(heap)
heappush(heap, -dur)
return len(heap)
Copy the code
The results
Given in the Python online submission to Course Schedule III. Memory Usage: Submissions in Python online submissions for Course Schedule III.Copy the code
Original link: leetcode.com/problems/co…
Your support is my biggest motivation