返回介绍

solution / 0600-0699 / 0630.Course Schedule III / README_EN

发布于 2024-06-17 01:03:36 字数 4947 浏览 0 评论 0 收藏 0

630. Course Schedule III

中文文档

Description

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.

Example 2:

Input: courses = [[1,2]]
Output: 1

Example 3:

Input: courses = [[3,2],[4,3]]
Output: 0

 

Constraints:

  • 1 <= courses.length <= 104
  • 1 <= durationi, lastDayi <= 104

Solutions

Solution 1

class Solution:
  def scheduleCourse(self, courses: List[List[int]]) -> int:
    courses.sort(key=lambda x: x[1])
    pq = []
    s = 0
    for duration, last in courses:
      heappush(pq, -duration)
      s += duration
      while s > last:
        s += heappop(pq)
    return len(pq)
class Solution {
  public int scheduleCourse(int[][] courses) {
    Arrays.sort(courses, (a, b) -> a[1] - b[1]);
    PriorityQueue<Integer> pq = new PriorityQueue<>((a, b) -> b - a);
    int s = 0;
    for (var e : courses) {
      int duration = e[0], last = e[1];
      pq.offer(duration);
      s += duration;
      while (s > last) {
        s -= pq.poll();
      }
    }
    return pq.size();
  }
}
class Solution {
public:
  int scheduleCourse(vector<vector<int>>& courses) {
    sort(courses.begin(), courses.end(), [](const vector<int>& a, const vector<int>& b) {
      return a[1] < b[1];
    });
    priority_queue<int> pq;
    int s = 0;
    for (auto& e : courses) {
      int duration = e[0], last = e[1];
      pq.push(duration);
      s += duration;
      while (s > last) {
        s -= pq.top();
        pq.pop();
      }
    }
    return pq.size();
  }
};
func scheduleCourse(courses [][]int) int {
  sort.Slice(courses, func(i, j int) bool { return courses[i][1] < courses[j][1] })
  pq := &hp{}
  s := 0
  for _, e := range courses {
    duration, last := e[0], e[1]
    s += duration
    pq.push(duration)
    for s > last {
      s -= pq.pop()
    }
  }
  return pq.Len()
}

type hp struct{ sort.IntSlice }

func (h hp) Less(i, j int) bool { return h.IntSlice[i] > h.IntSlice[j] }
func (h *hp) Push(v any)    { h.IntSlice = append(h.IntSlice, v.(int)) }
func (h *hp) Pop() any {
  a := h.IntSlice
  v := a[len(a)-1]
  h.IntSlice = a[:len(a)-1]
  return v
}
func (h *hp) push(v int) { heap.Push(h, v) }
func (h *hp) pop() int   { return heap.Pop(h).(int) }
function scheduleCourse(courses: number[][]): number {
  courses.sort((a, b) => a[1] - b[1]);
  const pq = new MaxPriorityQueue();
  let s = 0;
  for (const [duration, last] of courses) {
    pq.enqueue(duration);
    s += duration;
    while (s > last) {
      s -= pq.dequeue().element;
    }
  }
  return pq.size();
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文