FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
coding-Interview/CourseScheduleIII.java at master · arjunmullick/coding-Interview · GitHub
arjunmullick
/
coding-Interview
Public
Notifications
You must be signed in to change notification settings
Fork
1
Star
3
Code
Issues
0
Pull requests
0
Actions
Projects
Wiki
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Wiki
Security and quality
Insights
Expand file tree
Breadcrumbs
coding-Interview
/
CourseScheduleIII.java
Copy path
More file actions
More file actions
Latest commit
History
History
History
14 lines (14 loc) · 642 Bytes
Breadcrumbs
coding-Interview
/
CourseScheduleIII.java
Copy path
File metadata and controls
14 lines (14 loc) · 642 Bytes
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
public
class
Solution
{
public
int
scheduleCourse
(
int
[][]
courses
) {
Arrays
.
sort
(
courses
,(
a
,
b
)->
a
[
1
]-
b
[
1
]);
//Sort the courses by their deadlines (Greedy! We have to deal with courses with early deadlines first)
PriorityQueue
<
Integer
>
pq
=
new
PriorityQueue
<>((
a
,
b
)->
b
-
a
);
int
time
=
0
;
for
(
int
[]
c
:
courses
)
{
time
+=
c
[
0
];
// add current course to a priority queue
pq
.
add
(
c
[
0
]);
if
(
time
>
c
[
1
])
time
-=
pq
.
poll
();
//If time exceeds, drop the previous course which costs the most time. (That must be the best choice!)
}
return
pq
.
size
();
}
}
Back
|
FazBrowse Home
|
New Git URL