Movatterモバイル変換


[0]ホーム

URL:


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

Commit34c07bc

Browse files
refactor 630
1 parente577f94 commit34c07bc

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
publicclass_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-
publicintscheduleCourse(int[][]courses) {
34-
Arrays.sort(courses, (a,b) ->a[1] -b[1]);
35-
intday =0;
36-
PriorityQueue<Integer>maxHeap =newPriorityQueue<>((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+
publicstaticclassSolution1 {
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+
publicintscheduleCourse(int[][]courses) {
37+
Arrays.sort(courses, (a,b) ->a[1] -b[1]);
38+
intday =0;
39+
PriorityQueue<Integer>maxHeap =newPriorityQueue<>((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+
returnmaxHeap.size();
4348
}
44-
returnmaxHeap.size();
45-
}
46-
47-
publicstaticvoidmain(String...args) {
48-
_630test =new_630();
49-
int[][]courses =newint[][]{
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+
packagecom.fishercoder;
2+
3+
importcom.fishercoder.solutions._630;
4+
importorg.junit.BeforeClass;
5+
importorg.junit.Test;
6+
7+
importstaticorg.junit.Assert.assertEquals;
8+
9+
publicclass_630Test {
10+
privatestatic_630.Solution1solution1;
11+
privatestaticint[][]courses;
12+
13+
@BeforeClass
14+
publicstaticvoidsetup() {
15+
solution1 =new_630.Solution1();
16+
}
17+
18+
@Test
19+
publicvoidtest1() {
20+
courses =newint[][]{
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)

[8]ページ先頭

©2009-2025 Movatter.jp