File tree Expand file tree Collapse file tree 2 files changed +41
-0
lines changed Expand file tree Collapse file tree 2 files changed +41
-0
lines changed Original file line number Diff line number Diff line change
1
+ /**
2
+ * 630. Course Schedule III
3
+ * https://leetcode.com/problems/course-schedule-iii/
4
+ * Difficulty: Hard
5
+ *
6
+ * There are n different online courses numbered from 1 to n. You are given an array courses
7
+ * where courses[i] = [durationi, lastDayi] indicate that the ith course should be taken
8
+ * continuously for durationi days and must be finished before or on lastDayi.
9
+ *
10
+ * You will start on the 1st day and you cannot take two or more courses simultaneously.
11
+ *
12
+ * Return the maximum number of courses that you can take.
13
+ */
14
+
15
+ /**
16
+ * @param {number[][] } courses
17
+ * @return {number }
18
+ */
19
+ var scheduleCourse = function ( courses ) {
20
+ courses . sort ( ( a , b ) => a [ 1 ] - b [ 1 ] ) ;
21
+
22
+ const maxHeap = new PriorityQueue ( { compare : ( a , b ) => b - a } ) ;
23
+ let value = 0 ;
24
+
25
+ for ( const [ duration , lastDay ] of courses ) {
26
+ if ( value + duration <= lastDay ) {
27
+ value += duration ;
28
+ maxHeap . enqueue ( duration ) ;
29
+ } else {
30
+ if ( maxHeap . front ( ) > duration ) {
31
+ var prev = maxHeap . dequeue ( ) ;
32
+ value -= prev ;
33
+ value += duration ;
34
+ maxHeap . enqueue ( duration ) ;
35
+ }
36
+ }
37
+ }
38
+
39
+ return maxHeap . size ( ) ;
40
+ } ;
Original file line number Diff line number Diff line change 476
476
624|[ Maximum Distance in Arrays] ( ./0624-maximum-distance-in-arrays.js ) |Medium|
477
477
628|[ Maximum Product of Three Numbers] ( ./0628-maximum-product-of-three-numbers.js ) |Easy|
478
478
629|[ K Inverse Pairs Array] ( ./0629-k-inverse-pairs-array.js ) |Hard|
479
+ 630|[ Course Schedule III] ( ./0630-course-schedule-iii.js ) |Hard|
479
480
637|[ Average of Levels in Binary Tree] ( ./0637-average-of-levels-in-binary-tree.js ) |Easy|
480
481
643|[ Maximum Average Subarray I] ( ./0643-maximum-average-subarray-i.js ) |Easy|
481
482
645|[ Set Mismatch] ( ./0645-set-mismatch.js ) |Medium|
You can’t perform that action at this time.
0 commit comments