-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolution.java
More file actions
34 lines (29 loc) · 890 Bytes
/
Copy pathSolution.java
File metadata and controls
34 lines (29 loc) · 890 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
package programmers.greedy.sorting;
class Solution {
public int solution(int n, int[][] results) {
int answer = 0;
boolean[][] graph = new boolean[n + 1][n + 1];
for (int[] result : results) {
graph[result[0]][result[1]] = true;
}
for (int k = 1; k <= n; k++) {
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
if (graph[i][k] && graph[k][j]) {
graph[i][j] = true;
}
}
}
}
for (int i = 1; i <= n; i++) {
int count = n - 1;
for (int j = 1; j <= n; j++) {
if (i != j && (graph[i][j] || graph[j][i])) {
count--;
}
}
if (count == 0) answer++;
}
return answer;
}
}