https://leetcode.com/problems/find-if-path-exists-in-graph/description/
class Solution {
boolean[] vis;
List<List<Integer>> gr;
void dfs(int node) {
if (vis[node]) return;
vis[node] = true;
for (int child : gr.get(node)) {
if (!vis[child])
dfs(child);
}
}
public boolean validPath(int n, int[][] edges, int source, int destination) {
vis = new boolean[n];
gr = new ArrayList<>(n);
for (int i = 0; i < n; i++) {
gr.add(new ArrayList<>());
}
for (int[] edge : edges) {
int u = edge[0], v = edge[1];
gr.get(u).add(v);
gr.get(v).add(u);
}
dfs(source);
return vis[destination];
}
}
'CS > Algorism' 카테고리의 다른 글
99클럽 코테 스터디 26일차 TIL: [LeetCode] 275. H-Index II (0) | 2024.06.14 |
---|---|
[2023 KAKAO BLIND RECRUITMENT] 이모티콘 할인행사 (0) | 2024.06.14 |
[2023 KAKAO BLIND RECRUITMENT] 표 병합 (1) | 2024.06.13 |
99클럽 코테 스터디 24일차 TIL: [프로그래머스] 방의 개수 (0) | 2024.06.12 |
[2023 KAKAO BLIND RECRUITMENT] 개인정보 수집 유효기간 (0) | 2024.06.12 |