力扣刷题日记-面试题 630.课程最多

30 阅读1分钟
  • 将java代码改为js的过程中,该题我用数组排序sort方法,提交报超时,改成大顶堆就过了,就是写起来费事.
/**
 * @param {number[][]} courses
 * @return {number}
 */


 class BigHeap {
  constructor() {
    this.heap = []
  }
  getParentIndex(i) {
    // return Math.floor((i - 1) / 2)
    return (i - 1) >> 1
  }
  getLeftIndex(i) {
    return (i * 2) + 1
  }
  getRightIndex(i) {
    return (i * 2) + 2
  }
  shiftDown(index) {
    const leftIndex = this.getLeftIndex(index)
    const rightIndex = this.getRightIndex(index)
    if(this.heap[leftIndex] > this.heap[index]){
      this.swap(leftIndex,index)
      this.shiftDown(leftIndex)
    }
    if(this.heap[rightIndex] > this.heap[index]) {
      this.swap(rightIndex,index) 
      this.shiftDown(rightIndex)
    }
  }
  shiftUp(index) {
    if (index == 0) return
    const parentIndex = this.getParentIndex(index)
    if (this.heap[parentIndex] < this.heap[index]) {
      this.swap(parentIndex, index)
      this.shiftUp(parentIndex)
    }
  }
  swap(i1, i2) {
    const temp = this.heap[i1]
    this.heap[i1] = this.heap[i2]
    this.heap[i2] = temp
  }
  insert(value) {
    this.heap.push(value)
    this.shiftUp(this.heap.length - 1)
  }
  // 删除
  pop() {
    const t = this.heap[0]
    if(this.size() == 1) {
      this.heap = []
      return t
    }
    this.heap[0] = this.heap.pop()
    this.shiftDown(0)
    return t
  }
  // 获取堆顶
  peek() {
    return this.heap[0]
  }
  size() {
    return this.heap.length
  }
}
var scheduleCourse = function (courses) {
  let arr = courses.sort((a, b) => a[1] - b[1])
  let heap = new BigHeap() // 花费时间大根堆
  let time = 0;
  for (let c of courses) {
    if (time + c[0] <= c[1]) {
      // 当前时间 + 花费<= 截止时间
      time += c[0]
      heap.insert(c[0])
    } else {
      if (heap.size() != 0 && heap.peek() > c[0]) {
        time -= heap.peek();
        heap.pop()
        heap.insert(c[0])
        time += c[0]
      }
    }
  }
  return heap.size()
};

java版本:

package class51;
import java.util.Arrays;
import java.util.PriorityQueue;
public class Problem_0630_CourseScheduleIII {
  public static int scheduleCourse(int[][] courses) {
    // courses[i] = [花费时间,截止时间]
    Arrays.sort(courses, (a, b) -> a[1] - b[1]);
    // 花费时间的大根堆
    PriorityQueue<Integer> heap = new PriorityQueue<>((a,b) -> b - a)
    // 时间点
    int time = 0;
    for(int[] c : courses) {
      if(time + c[0] <= c[1]) {
        // 当前时间 + 花费时间 <= 截止时间
        heap.add(c[0])
        time += c[0]
      }else {
        // 当前时间 + 花费时间 > 截止时间的,只有淘汰掉某课程,当前的课程才能进来
        if(!heap.isEmpty() && heap.peek() > c[0]) {
          time -= heap.poll();
          heap.add(c[0])
          time += c[0]
        }
      }
    }
    return heap.size()
  }
}

`