Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 7014b52

Browse files
committed
Add solution #630
1 parent 0eb2657 commit 7014b52

File tree

2 files changed

+41
-0
lines changed

2 files changed

+41
-0
lines changed

0630-course-schedule-iii.js

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
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+
};

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -476,6 +476,7 @@
476476
624|[Maximum Distance in Arrays](./0624-maximum-distance-in-arrays.js)|Medium|
477477
628|[Maximum Product of Three Numbers](./0628-maximum-product-of-three-numbers.js)|Easy|
478478
629|[K Inverse Pairs Array](./0629-k-inverse-pairs-array.js)|Hard|
479+
630|[Course Schedule III](./0630-course-schedule-iii.js)|Hard|
479480
637|[Average of Levels in Binary Tree](./0637-average-of-levels-in-binary-tree.js)|Easy|
480481
643|[Maximum Average Subarray I](./0643-maximum-average-subarray-i.js)|Easy|
481482
645|[Set Mismatch](./0645-set-mismatch.js)|Medium|

0 commit comments

Comments
 (0)