Skip to content

Commit 34c07bc

Browse files
refactor 630
1 parent e577f94 commit 34c07bc

File tree

2 files changed

+46
-25
lines changed

2 files changed

+46
-25
lines changed

src/main/java/com/fishercoder/solutions/_630.java

Lines changed: 16 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -28,32 +28,23 @@ Given n online courses represented by pairs (t,d), your task is to find the maxi
2828
You can't take two courses simultaneously.
2929
*/
3030
public class _630 {
31-
/**Reference: https://discuss.leetcode.com/topic/93790/short-java-code-using-priorityqueue
32-
* Sort by finish date!!! This is greedy! We should take those classes that finish early first.*/
33-
public int scheduleCourse(int[][] courses) {
34-
Arrays.sort(courses, (a,b) -> a[1] - b[1]);
35-
int day = 0;
36-
PriorityQueue<Integer> maxHeap = new PriorityQueue<>((a, b) -> b - a);
37-
for (int[] course : courses) {
38-
day += course[0];
39-
maxHeap.offer(course[0]);
40-
if (day > course[1]) {
41-
day -= maxHeap.poll();//drop the previous courses that took the most time
31+
public static class Solution1 {
32+
/**
33+
* Reference: https://discuss.leetcode.com/topic/93790/short-java-code-using-priorityqueue
34+
* Sort by finish date!!! This is greedy! We should take those classes that finish early first.
35+
*/
36+
public int scheduleCourse(int[][] courses) {
37+
Arrays.sort(courses, (a, b) -> a[1] - b[1]);
38+
int day = 0;
39+
PriorityQueue<Integer> maxHeap = new PriorityQueue<>((a, b) -> b - a);
40+
for (int[] course : courses) {
41+
day += course[0];
42+
maxHeap.offer(course[0]);
43+
if (day > course[1]) {
44+
day -= maxHeap.poll();//drop the previous courses that took the most time
45+
}
4246
}
47+
return maxHeap.size();
4348
}
44-
return maxHeap.size();
45-
}
46-
47-
public static void main(String... args) {
48-
_630 test = new _630();
49-
int[][] courses = new int[][]{
50-
{100, 200},
51-
{200, 1300},
52-
{1000, 1250},
53-
{2000, 3200},
54-
{300, 1200}
55-
};
56-
test.scheduleCourse(courses);
57-
System.out.println("Finished..");
5849
}
5950
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.solutions._630;
4+
import org.junit.BeforeClass;
5+
import org.junit.Test;
6+
7+
import static org.junit.Assert.assertEquals;
8+
9+
public class _630Test {
10+
private static _630.Solution1 solution1;
11+
private static int[][] courses;
12+
13+
@BeforeClass
14+
public static void setup() {
15+
solution1 = new _630.Solution1();
16+
}
17+
18+
@Test
19+
public void test1() {
20+
courses = new int[][]{
21+
{100, 200},
22+
{200, 1300},
23+
{1000, 1250},
24+
{2000, 3200},
25+
{300, 1200}
26+
};
27+
assertEquals(3, solution1.scheduleCourse(courses));
28+
}
29+
30+
}

0 commit comments

Comments
 (0)