Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 41feb6d

Browse files
authored
Improved task 630.
1 parent 78287f9 commit 41feb6d

File tree

2 files changed

+49
-69
lines changed

2 files changed

+49
-69
lines changed

‎src/main/java/g0601_0700/s0630_course_schedule_iii/Solution.java

Lines changed: 36 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -3,83 +3,50 @@
33
// #Hard #Array #Greedy #Heap_Priority_Queue
44

55
import java.util.Arrays;
6+
import java.util.PriorityQueue;
67

78
@SuppressWarnings("java:S135")
89
public class Solution {
910
public int scheduleCourse(int[][] courses) {
11+
// Sort the courses based on their deadline date.
1012
Arrays.sort(courses, (a, b) -> a[1] - b[1]);
11-
int course = 0;
13+
// Only the duration is stored. We don't care which course
14+
// is the longest, we only care about the total courses can
15+
// be taken.
16+
// If the question wants the course ids to be returned.
17+
// Consider use a Pair<Duration, CourseId> int pair.
18+
PriorityQueue<Integer> pq = new PriorityQueue<>((a, b) -> b - a);
19+
// Total time consumed.
1220
int time = 0;
13-
MaxHeap heap = new MaxHeap(courses.length);
14-
for (int[] cours : courses) {
15-
if (cours[1] - time >= cours[0]) {
16-
time += cours[0];
17-
course++;
18-
heap.add(cours[0]);
19-
} else if (cours[0] < heap.getHeap()[0]) {
20-
int t = heap.pop();
21-
heap.add(cours[0]);
22-
time = time - t + cours[0];
23-
}
24-
}
25-
return course;
26-
}
27-
28-
static class MaxHeap {
29-
private final int[] heap;
30-
private int pin;
31-
32-
public MaxHeap(int mexLen) {
33-
this.heap = new int[mexLen];
34-
this.pin = 0;
35-
}
36-
37-
public int[] getHeap() {
38-
return heap;
39-
}
40-
41-
public void add(int e) {
42-
heap[pin] = e;
43-
int temp = pin;
44-
pin++;
45-
while (temp > 0 && heap[(temp - 1) / 2] < e) {
46-
heap[temp] = heap[(temp - 1) / 2];
47-
temp = (temp - 1) / 2;
48-
heap[temp] = e;
49-
}
50-
}
51-
52-
public int pop() {
53-
int res = heap[0];
54-
pin--;
55-
heap[0] = heap[pin];
56-
int h0 = heap[0];
57-
int temp = 0;
58-
while (temp * 2 + 1 < pin) {
59-
if (temp * 2 + 2 == pin) {
60-
if (heap[temp * 2 + 1] > h0) {
61-
heap[temp] = heap[temp * 2 + 1];
62-
temp = temp * 2 + 1;
63-
heap[temp] = h0;
64-
} else {
65-
break;
66-
}
67-
} else {
68-
if (h0 < heap[temp * 2 + 1] || h0 < heap[temp * 2 + 2]) {
69-
if (heap[temp * 2 + 1] > heap[temp * 2 + 2]) {
70-
heap[temp] = heap[temp * 2 + 1];
71-
temp = temp * 2 + 1;
72-
} else {
73-
heap[temp] = heap[temp * 2 + 2];
74-
temp = temp * 2 + 2;
75-
}
76-
heap[temp] = h0;
77-
} else {
78-
break;
79-
}
21+
// At the given time `course`, the overall "time limit" is
22+
// course[1]. All courses in pq is already 'valid'. But
23+
// adding this course[0] might exceed the course[1] limit.
24+
for (int[] course : courses) {
25+
// If adding this course doesn't exceed. Let's add it
26+
// for now. (Greedy algo). We might remove it later if
27+
// we have a "better" solution at that time.
28+
if (time + course[0] <= course[1]) {
29+
time += course[0];
30+
pq.offer(course[0]);
31+
} else {
32+
// If adding this ecxeeds the limit. We can still add it
33+
// if-and-only-if there are courses longer than current
34+
// one. If so, by removing a longer course, current shorter
35+
// course can fit in for sure. Although the total course
36+
// count is the same, the overall time consumed is shorter.
37+
// Which gives us more room for future courses.
38+
// Remove any course that is longer than current course
39+
// will work, but we remove the longest one with the help
40+
// of heap (pq).
41+
if (!pq.isEmpty() && pq.peek() > course[0]) {
42+
time -= pq.poll();
43+
time += course[0];
44+
pq.offer(course[0]);
8045
}
46+
// If no course in consider (pq) is shorter than the
47+
// current course. It is safe to discard it.
8148
}
82-
return res;
8349
}
50+
return pq.size();
8451
}
8552
}

‎src/test/java/g0601_0700/s0630_course_schedule_iii/SolutionTest.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,17 @@ void scheduleCourse2() {
2121
void scheduleCourse3() {
2222
assertThat(new Solution().scheduleCourse(new int[][] {{3, 2}, {4, 3}}), equalTo(0));
2323
}
24+
25+
@Test
26+
void scheduleCourse4() {
27+
int[][] input =
28+
new int[][] {
29+
{100, 200},
30+
{200, 1300},
31+
{1000, 1250},
32+
{2000, 3200},
33+
{300, 1200}
34+
};
35+
assertThat(new Solution().scheduleCourse(input), equalTo(4));
36+
}
2437
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /