All Downloads are FREE. Search and download functionalities are using the official Maven repository.

g1401_1500.s1462_course_schedule_iv.Solution Maven / Gradle / Ivy

There is a newer version: 1.38
Show newest version
package g1401_1500.s1462_course_schedule_iv;

// #Medium #Depth_First_Search #Breadth_First_Search #Graph #Topological_Sort
// #2022_03_29_Time_10_ms_(100.00%)_Space_46.3_MB_(94.57%)

import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Queue;

/**
 * 1462 - Course Schedule IV\.
 *
 * Medium
 *
 * There are a total of `numCourses` courses you have to take, labeled from `0` to `numCourses - 1`. You are given an array `prerequisites` where prerequisites[i] = [ai, bi] indicates that you **must** take course ai first if you want to take course bi.
 *
 * *   For example, the pair `[0, 1]` indicates that you have to take course `0` before you can take course `1`.
 *
 * Prerequisites can also be **indirect**. If course `a` is a prerequisite of course `b`, and course `b` is a prerequisite of course `c`, then course `a` is a prerequisite of course `c`.
 *
 * You are also given an array `queries` where queries[j] = [uj, vj]. For the jth query, you should answer whether course uj is a prerequisite of course vj or not.
 *
 * Return _a boolean array_ `answer`_, where_ `answer[j]` _is the answer to the_ jth _query._
 *
 * **Example 1:**
 *
 * ![](https://assets.leetcode.com/uploads/2021/05/01/courses4-1-graph.jpg)
 *
 * **Input:** numCourses = 2, prerequisites = \[\[1,0]], queries = \[\[0,1],[1,0]]
 *
 * **Output:** [false,true]
 *
 * **Explanation:** The pair [1, 0] indicates that you have to take course 1 before you can take course 0. Course 0 is not a prerequisite of course 1, but the opposite is true.
 *
 * **Example 2:**
 *
 * **Input:** numCourses = 2, prerequisites = [], queries = \[\[1,0],[0,1]]
 *
 * **Output:** [false,false]
 *
 * **Explanation:** There are no prerequisites, and each course is independent.
 *
 * **Example 3:**
 *
 * ![](https://assets.leetcode.com/uploads/2021/05/01/courses4-3-graph.jpg)
 *
 * **Input:** numCourses = 3, prerequisites = \[\[1,2],[1,0],[2,0]], queries = \[\[1,0],[1,2]]
 *
 * **Output:** [true,true]
 *
 * **Constraints:**
 *
 * *   `2 <= numCourses <= 100`
 * *   `0 <= prerequisites.length <= (numCourses * (numCourses - 1) / 2)`
 * *   `prerequisites[i].length == 2`
 * *   0 <= ai, bi <= n - 1
 * *   ai != bi
 * *   All the pairs [ai, bi] are **unique**.
 * *   The prerequisites graph has no cycles.
 * *   1 <= queries.length <= 104
 * *   0 <= ui, vi <= n - 1
 * *   ui != vi
**/
public class Solution {
    public List checkIfPrerequisite(
            int numCourses, int[][] prerequisites, int[][] queries) {
        Map> m = new HashMap<>();
        int[] ind = new int[numCourses];
        for (int[] p : prerequisites) {
            m.computeIfAbsent(p[1], o -> new ArrayList<>()).add(p[0]);
            ind[p[0]]++;
        }
        boolean[][] r = new boolean[numCourses][numCourses];
        Queue q = new LinkedList<>();
        for (int i = 0; i < numCourses; i++) {
            if (ind[i] == 0) {
                q.add(i);
            }
        }
        while (!q.isEmpty()) {
            int j = q.poll();
            for (int k : m.getOrDefault(j, new ArrayList<>())) {
                r[k][j] = true;
                for (int l = 0; l < r.length; l++) {
                    if (r[j][l]) {
                        r[k][l] = true;
                    }
                }
                ind[k]--;
                if (ind[k] == 0) {
                    q.offer(k);
                }
            }
        }
        List a = new ArrayList<>();
        for (int[] qr : queries) {
            a.add(r[qr[0]][qr[1]]);
        }
        return a;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy