@@ -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 */
3030public 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}
0 commit comments