Skip to content

Commit e586271

Browse files
author
Aarzoo
committed
add solutions, explanation and update the README file of the day 1462
1 parent ae6e8dc commit e586271

File tree

8 files changed

+256
-2
lines changed

8 files changed

+256
-2
lines changed

Algorithms/README.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,8 @@
4242
| 1400 | Construct K Palindrome Strings | [C++](./Algorithms/src/1400.%20Construct%20K%20Palindrome%20Strings/Code/solution.cpp), [Java](./Algorithms/src/1400.%20Construct%20K%20Palindrome%20Strings/Code/solution.java), [JavaScript](./Algorithms/src/1400.%20Construct%20K%20Palindrome%20Strings/Code/solution.js), [Python](./Algorithms/src/1400.%20Construct%20K%20Palindrome%20Strings/Code/solution.py), [Go](./Algorithms/src/1400.%20Construct%20K%20Palindrome%20Strings/Code/solution.go) | [Explanation](./Algorithms/src/1400.%20Construct%20K%20Palindrome%20Strings/Explanation/explanation.md) | Medium |
4343
| 1405 | Longest Happy String | [C++](./Algorithms/src/1405.%20Longest%20Happy%20String/Code/solution.cpp), [Java](./Algorithms/src/1405.%20Longest%20Happy%20String/Code/solution.java), [JavaScript](./Algorithms/src/1405.%20Longest%20Happy%20String/Code/solution.js), [Python](./Algorithms/src/1405.%20Longest%20Happy%20String/Code/solution.py), [Go](./Algorithms/src/1405.%20Longest%20Happy%20String/Code/solution.go) | [Explanation](./Algorithms/src/1405.%20Longest%20Happy%20String/Explanation/explanation.md) | Medium |
4444
| 1408 | String Matching in an Array | [C++](./Algorithms/src/1408.%20String%20Matching%20in%20an%20Array/Code/solution.cpp), [Java](./Algorithms/src/1408.%20String%20Matching%20in%20an%20Array/Code/solution.java), [JavaScript](./Algorithms/src/1408.%20String%20Matching%20in%20an%20Array/Code/solution.js), [Python](./Algorithms/src/1408.%20String%20Matching%20in%20an%20Array/Code/solution.py), [Go](./Algorithms/src/1408.%20String%20Matching%20in%20an%20Array/Code/solution.go) | [Explanation](./Algorithms/src/1408.%20String%20Matching%20in%20an%20Array/Explanation/explanation.md) | Easy |
45-
| 1460 | Make Two Arrays Equal by Reversing Subarrays | [C++](./Algorithms/src/1460.%20Make%20Two%20Arrays%20Equal%20by%20Reversing%20Subarrays/Code/solution.cpp), [Java](./Algorithms/src/1460.%20Make%20Two%20Arrays%20Equal%20by%20Reversing%20Subarrays/Code/solution.java), [JavaScript](./Algorithms/src/1460.%20Make%20Two%20Arrays%20Equal%20by%20Reversing%20Subarrays/Code/solution.js), [Python](./Algorithms/src/1460.%20Make%20Two%20Arrays%20Equal%20by%20Reversing%20Subarrays/Code/solution.py), [Go](./Algorithms/src/1598.1460.%20Make%20Two%20Arrays%20Equal%20by%20Reversing%20Subarrays/solution.go) | [Explanation](./Algorithms/src/1460.%20Make%20Two%20Arrays%20Equal%20by%20Reversing%20Subarrays/Explanation/explanation.md) | Easy |
45+
| 1460 | Make Two Arrays Equal by Reversing Subarrays | [C++](./Algorithms/src/1460.%20Make%20Two%20Arrays%20Equal%20by%20Reversing%20Subarrays/Code/solution.cpp), [Java](./Algorithms/src/1460.%20Make%20Two%20Arrays%20Equal%20by%20Reversing%20Subarrays/Code/solution.java), [JavaScript](./Algorithms/src/1460.%20Make%20Two%20Arrays%20Equal%20by%20Reversing%20Subarrays/Code/solution.js), [Python](./Algorithms/src/1460.%20Make%20Two%20Arrays%20Equal%20by%20Reversing%20Subarrays/Code/solution.py), [Go](./Algorithms/src/1598.1460.%20Make%20Two%20Arrays%20Equal%20by%20Reversing%20Subarrays/Code/solution.go) | [Explanation](./Algorithms/src/1460.%20Make%20Two%20Arrays%20Equal%20by%20Reversing%20Subarrays/Explanation/explanation.md) | Easy |
46+
| 1462 | Course Schedule IV | [C++](./Algorithms/src/1462.%20Course%20Schedule%20IV/Code/solution.cpp), [Java](./Algorithms/src/1462.%20Course%20Schedule%20IV/Code/solution.java), [JavaScript](./Algorithms/src/1462.%20Course%20Schedule%20IV/Code/solution.js), [Python](./Algorithms/src/1462.%20Course%20Schedule%20IV/Code/solution.py), [Go](./Algorithms/src/1598.1462.%20Course%20Schedule%20IV/Code/solution.go) | [Explanation](./Algorithms/src/1462.%20Course%20Schedule%20IV/Explanation/explanation.md) | Medium |
4647
| 1497 | Check If Array Pairs Are Divisible by k | [C++](./Algorithms/src/1497.%20Check%20If%20Array%20Pairs%20Are%20Divisible%20by%20k/Code/solution.cpp), [Java](./Algorithms/src/1497.%20Check%20If%20Array%20Pairs%20Are%20Divisible%20by%20k/Code/solution.java), [JavaScript](./Algorithms/src/1497.%20Check%20If%20Array%20Pairs%20Are%20Divisible%20by%20k/Code/solution.js), [Python](./Algorithms/src/1497.%20Check%20If%20Array%20Pairs%20Are%20Divisible%20by%20k/Code/solution.py), [Go](./Algorithms/src/1598.1497.%20Check%20If%20Array%20Pairs%20Are%20Divisible%20by%20k/solution.go) | [Explanation](./Algorithms/src/1497.%20Check%20If%20Array%20Pairs%20Are%20Divisible%20by%20k/Explanation/explanation.md) | Medium |
4748
| 1509 | Minimum Difference Between Largest and Smallest Value in Three Moves | [C++](./Algorithms/src/1509.%20Minimum%20Difference%20Between%20Largest%20and%20Smallest%20Value%20in%20Three%20Moves/Code/solution.cpp), [Java](./Algorithms/src/1509.%20Minimum%20Difference%20Between%20Largest%20and%20Smallest%20Value%20in%20Three%20Moves/Code/solution.java), [JavaScript](./Algorithms/src/1509.%20Minimum%20Difference%20Between%20Largest%20and%20Smallest%20Value%20in%20Three%20Moves/Code/solution.js), [Python](./Algorithms/src/1509.%20Minimum%20Difference%20Between%20Largest%20and%20Smallest%20Value%20in%20Three%20Moves/Code/solution.py), [Go](./Algorithms/src/1509.%20Minimum%20Difference%20Between%20Largest%20and%20Smallest%20Value%20in%20Three%20Moves/Code/solution.go) | [Explanation](./Algorithms/src/1509.%20Minimum%20Difference%20Between%20Largest%20and%20Smallest%20Value%20in%20Three%20Moves/Explanation/explanation.md) | Medium |
4849
| 1545 | Find Kth Bit in Nth Binary String | [C++](./Algorithms/src/1545.%20Find%20Kth%20Bit%20in%20Nth%20Binary%20String/Code/solution.cpp), [Java](./Algorithms/src/1545.%20Find%20Kth%20Bit%20in%20Nth%20Binary%20String/Code/solution.java), [JavaScript](./Algorithms/src/1545.%20Find%20Kth%20Bit%20in%20Nth%20Binary%20String/Code/solution.js), [Python](./Algorithms/src/1545.%20Find%20Kth%20Bit%20in%20Nth%20Binary%20String/Code/solution.py), [Go](./Algorithms/src/1545.%20Find%20Kth%20Bit%20in%20Nth%20Binary%20String/Code/solution.go) | [Explanation](./Algorithms/src/1545.%20Find%20Kth%20Bit%20in%20Nth%20Binary%20String/Explanation/explanation.md) | Medium |
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
class Solution
2+
{
3+
public:
4+
vector<bool> checkIfPrerequisite(int numCourses, vector<vector<int>> &prerequisites, vector<vector<int>> &queries)
5+
{
6+
// Initialize the graph
7+
vector<vector<bool>> graph(numCourses, vector<bool>(numCourses, false));
8+
9+
// Build the direct edges from prerequisites
10+
for (const auto &edge : prerequisites)
11+
{
12+
graph[edge[0]][edge[1]] = true;
13+
}
14+
15+
// Floyd-Warshall to compute transitive closure
16+
for (int k = 0; k < numCourses; ++k)
17+
{
18+
for (int i = 0; i < numCourses; ++i)
19+
{
20+
for (int j = 0; j < numCourses; ++j)
21+
{
22+
if (graph[i][k] && graph[k][j])
23+
{
24+
graph[i][j] = true;
25+
}
26+
}
27+
}
28+
}
29+
30+
// Answer the queries
31+
vector<bool> result;
32+
for (const auto &query : queries)
33+
{
34+
result.push_back(graph[query[0]][query[1]]);
35+
}
36+
37+
return result;
38+
}
39+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
func checkIfPrerequisite(numCourses int, prerequisites [][]int, queries [][]int) []bool {
2+
// Initialize the graph
3+
graph := make([][]bool, numCourses)
4+
for i := 0; i < numCourses; i++ {
5+
graph[i] = make([]bool, numCourses)
6+
}
7+
8+
// Build the direct edges from prerequisites
9+
for _, edge := range prerequisites {
10+
graph[edge[0]][edge[1]] = true
11+
}
12+
13+
// Floyd-Warshall to compute transitive closure
14+
for k := 0; k < numCourses; k++ {
15+
for i := 0; i < numCourses; i++ {
16+
for j := 0; j < numCourses; j++ {
17+
if graph[i][k] && graph[k][j] {
18+
graph[i][j] = true
19+
}
20+
}
21+
}
22+
}
23+
24+
// Answer the queries
25+
result := make([]bool, len(queries))
26+
for i, query := range queries {
27+
result[i] = graph[query[0]][query[1]]
28+
}
29+
30+
return result
31+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
class Solution {
2+
public List<Boolean> checkIfPrerequisite(int numCourses, int[][] prerequisites, int[][] queries) {
3+
// Initialize the graph
4+
boolean[][] graph = new boolean[numCourses][numCourses];
5+
6+
// Build the direct edges from prerequisites
7+
for (int[] edge : prerequisites) {
8+
graph[edge[0]][edge[1]] = true;
9+
}
10+
11+
// Floyd-Warshall to compute transitive closure
12+
for (int k = 0; k < numCourses; k++) {
13+
for (int i = 0; i < numCourses; i++) {
14+
for (int j = 0; j < numCourses; j++) {
15+
if (graph[i][k] && graph[k][j]) {
16+
graph[i][j] = true;
17+
}
18+
}
19+
}
20+
}
21+
22+
// Answer the queries
23+
List<Boolean> result = new ArrayList<>();
24+
for (int[] query : queries) {
25+
result.add(graph[query[0]][query[1]]);
26+
}
27+
28+
return result;
29+
}
30+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
var checkIfPrerequisite = function (numCourses, prerequisites, queries) {
2+
// Initialize the graph
3+
const graph = Array.from({ length: numCourses }, () =>
4+
Array(numCourses).fill(false)
5+
);
6+
7+
// Build the direct edges from prerequisites
8+
for (const [u, v] of prerequisites) {
9+
graph[u][v] = true;
10+
}
11+
12+
// Floyd-Warshall to compute transitive closure
13+
for (let k = 0; k < numCourses; k++) {
14+
for (let i = 0; i < numCourses; i++) {
15+
for (let j = 0; j < numCourses; j++) {
16+
if (graph[i][k] && graph[k][j]) {
17+
graph[i][j] = true;
18+
}
19+
}
20+
}
21+
}
22+
23+
// Answer the queries
24+
return queries.map(([u, v]) => graph[u][v]);
25+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
class Solution:
2+
def checkIfPrerequisite(self, numCourses: int, prerequisites: List[List[int]], queries: List[List[int]]) -> List[bool]:
3+
# Initialize the graph
4+
graph = [[False] * numCourses for _ in range(numCourses)]
5+
6+
# Build the direct edges from prerequisites
7+
for u, v in prerequisites:
8+
graph[u][v] = True
9+
10+
# Floyd-Warshall to compute transitive closure
11+
for k in range(numCourses):
12+
for i in range(numCourses):
13+
for j in range(numCourses):
14+
if graph[i][k] and graph[k][j]:
15+
graph[i][j] = True
16+
17+
# Answer the queries
18+
return [graph[u][v] for u, v in queries]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
# Problem: Check If a Prerequisite Exists
2+
3+
This repository provides solutions to the problem "Check If a Prerequisite Exists" implemented in **C++**, **Java**, **JavaScript**, **Python**, and **Go**. Below, you'll find step-by-step explanations for each language.
4+
5+
---
6+
7+
## C++ Code Implementation: Step-by-Step
8+
9+
1. **Initialize the Adjacency Matrix**
10+
Start by initializing a 2D matrix to store whether a course is a prerequisite of another course.
11+
12+
2. **Set up the Prerequisite Relationships**
13+
Using the prerequisites list, update the adjacency matrix with direct relationships between courses.
14+
15+
3. **Floyd-Warshall Algorithm**
16+
Apply the Floyd-Warshall algorithm to compute all pairs' reachability. This updates the matrix to reflect indirect prerequisites.
17+
18+
4. **Process the Queries**
19+
For each query, check the adjacency matrix to see if one course is a prerequisite of another.
20+
21+
---
22+
23+
## Java Code Implementation: Step-by-Step
24+
25+
1. **Prepare the Adjacency Matrix**
26+
Create a 2D array to store whether a course is a prerequisite for another.
27+
28+
2. **Update Relationships from Prerequisites**
29+
Populate the matrix with the prerequisites directly given in the input.
30+
31+
3. **Use Floyd-Warshall Algorithm**
32+
Implement Floyd-Warshall to ensure all transitive prerequisites are captured in the matrix.
33+
34+
4. **Answer the Queries**
35+
For each query, use the matrix to determine if the prerequisite relationship exists.
36+
37+
---
38+
39+
## JavaScript Code Implementation: Step-by-Step
40+
41+
1. **Build the Graph**
42+
Create a graph representation using a 2D array to represent the prerequisite relationships.
43+
44+
2. **Populate Direct Relationships**
45+
Populate the graph with direct prerequisites using the input data.
46+
47+
3. **Compute Transitive Closure**
48+
Utilize the Floyd-Warshall algorithm to compute transitive relationships between courses.
49+
50+
4. **Evaluate Queries**
51+
Loop through each query and return whether the course relationship exists.
52+
53+
---
54+
55+
## Python Code Implementation: Step-by-Step
56+
57+
1. **Initialize a 2D Matrix**
58+
Create a matrix where `matrix[i][j]` indicates whether course `i` is a prerequisite for course `j`.
59+
60+
2. **Update the Matrix for Direct Prerequisites**
61+
Populate the matrix with the relationships provided in the prerequisites list.
62+
63+
3. **Apply Floyd-Warshall Algorithm**
64+
Implement the Floyd-Warshall algorithm to find all indirect relationships between courses.
65+
66+
4. **Answer the Queries**
67+
For each query, check the value in the matrix to determine if the prerequisite exists.
68+
69+
---
70+
71+
## Go Code Implementation: Step-by-Step
72+
73+
1. **Set up the Graph as a 2D Slice**
74+
Initialize a 2D slice to represent the adjacency matrix of course relationships.
75+
76+
2. **Add Direct Relationships**
77+
Update the graph with the direct prerequisites based on the input data.
78+
79+
3. **Floyd-Warshall Algorithm for Transitive Closure**
80+
Implement Floyd-Warshall to propagate indirect prerequisites across the matrix.
81+
82+
4. **Handle Queries**
83+
For each query, determine whether a course is a prerequisite by checking the matrix.
84+
85+
---
86+
87+
Each implementation shares a common approach:
88+
89+
1. Build a representation of prerequisites.
90+
2. Compute all transitive relationships using the Floyd-Warshall algorithm.
91+
3. Check the matrix to answer queries.
92+
93+
For the complete implementation, refer to the respective files:
94+
95+
- `solution.cpp`
96+
- `solution.java`
97+
- `solution.js`
98+
- `solution.py`
99+
- `solution.go`
100+
101+
---
102+
103+
### Complexity Analysis
104+
105+
- **Time Complexity**:
106+
The Floyd-Warshall algorithm takes $$O(n^3)$$ time, where \(n\) is the number of courses. Processing the queries takes $$O(q)$$, where \(q\) is the number of queries.
107+
108+
- **Space Complexity**:
109+
The space complexity is $$O(n^2)$$ for the adjacency matrix.

0 commit comments

Comments
 (0)