Skip to content

Commit c2cd647

Browse files
committedJan 27, 2025
Add solution #1462
1 parent 2329c58 commit c2cd647

File tree

2 files changed

+44
-0
lines changed

2 files changed

+44
-0
lines changed
 

‎README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -392,6 +392,7 @@
392392
1455|[Check If a Word Occurs As a Prefix of Any Word in a Sentence](./1455-check-if-a-word-occurs-as-a-prefix-of-any-word-in-a-sentence.js)|Easy|
393393
1456|[Maximum Number of Vowels in a Substring of Given Length](./1456-maximum-number-of-vowels-in-a-substring-of-given-length.js)|Medium|
394394
1460|[Make Two Arrays Equal by Reversing Sub-arrays](./1460-make-two-arrays-equal-by-reversing-sub-arrays.js)|Easy|
395+
1462|[Course Schedule IV](./1462-course-schedule-iv.js)|Medium|
395396
1464|[Maximum Product of Two Elements in an Array](./1464-maximum-product-of-two-elements-in-an-array.js)|Easy|
396397
1466|[Reorder Routes to Make All Paths Lead to the City Zero](./1466-reorder-routes-to-make-all-paths-lead-to-the-city-zero.js)|Medium|
397398
1470|[Shuffle the Array](./1470-shuffle-the-array.js)|Easy|

‎solutions/1462-course-schedule-iv.js

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/**
2+
* 1462. Course Schedule IV
3+
* https://leetcode.com/problems/course-schedule-iv/
4+
* Difficulty: Medium
5+
*
6+
* There are a total of numCourses courses you have to take, labeled from 0 to numCourses - 1.
7+
* You are given an array prerequisites where prerequisites[i] = [ai, bi] indicates that you
8+
* must take course ai first if you want to take course bi.
9+
*
10+
* - For example, the pair [0, 1] indicates that you have to take course 0 before you can take
11+
* course 1.
12+
*
13+
* Prerequisites can also be indirect. If course a is a prerequisite of course b, and course b
14+
* is a prerequisite of course c, then course a is a prerequisite of course c.
15+
*
16+
* You are also given an array queries where queries[j] = [uj, vj]. For the jth query, you
17+
* should answer whether course uj is a prerequisite of course vj or not.
18+
*
19+
* Return a boolean array answer, where answer[j] is the answer to the jth query.
20+
*/
21+
22+
/**
23+
* @param {number} numCourses
24+
* @param {number[][]} prerequisites
25+
* @param {number[][]} queries
26+
* @return {boolean[]}
27+
*/
28+
var checkIfPrerequisite = function(numCourses, prerequisites, queries) {
29+
const lookup = new Array(numCourses).fill().map(() => new Array(numCourses).fill(false));
30+
prerequisites.forEach(([u, v]) => lookup[u][v] = true);
31+
32+
for (let i = 0; i < numCourses; i++) {
33+
for (let j = 0; j < numCourses; j++) {
34+
for (let k = 0; k < numCourses; k++) {
35+
if (lookup[j][i] && lookup[i][k]) {
36+
lookup[j][k] = true;
37+
}
38+
}
39+
}
40+
}
41+
42+
return queries.map(([u, v]) => lookup[u][v]);
43+
};

0 commit comments

Comments
 (0)
Please sign in to comment.