This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Solution: | |
def eventualSafeNodes(self, graph: List[List[int]]) -> List[int]: | |
def dfs(curr, vis, path): | |
vis[curr] = 1 | |
path[curr] = 1 | |
for u in graph[curr]: | |
if vis[u] == 0: | |
if dfs(u, vis, path): | |
return True |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Solution { | |
public List<Integer> eventualSafeNodes(int[][] graph) { | |
int n = graph.length; | |
int[] vis = new int[n]; | |
int[] path = new int[n]; | |
List<Integer> res = new ArrayList<>(); | |
for (int i = 0; i < n; i++) { | |
if (vis[i] == 0) { | |
dfs(i, graph, vis, path); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Solution: | |
def findCircleNum(self, edges: List[List[int]]) -> int: | |
n = len(edges) | |
root = [i for i in range(n)] | |
rank = [1] * n | |
def find(i): | |
r = root[i] | |
while r != root[r]: | |
root[r] = root[root[r]] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.*; | |
class Solution { | |
private int[] root; | |
private int[] rank; | |
public int findCircleNum(int[][] edges) { | |
int n = edges.length; | |
root = new int[n]; | |
rank = new int[n]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Solution: | |
def allPathsSourceTarget(self, graph: List[List[int]]) -> List[List[int]]: | |
def dfs(i, path): | |
if i == len(graph) - 1: res.append(path) | |
else: | |
for nxt in graph[i]: dfs(nxt, path + [nxt]) | |
res = [] | |
dfs(0, [0]) | |
return res |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Solution { | |
public List<List<Integer>> allPathsSourceTarget(int[][] graph) { | |
List<List<Integer>> res = new ArrayList<>(); | |
helper(graph, 0, new ArrayList<>(), res); | |
return res; | |
} | |
private void helper(int[][] graph, int i, List<Integer> path, List<List<Integer>> res) { | |
path.add(i); | |
if (i == graph.length - 1) { | |
res.add(new ArrayList<Integer>(path)); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Solution: | |
def shortestBridge(self, A: List[List[int]]) -> int: | |
r, c = len(A), len(A[0]) | |
start = set() | |
dirs = [(-1, 0), (0, -1), (0, 1), (1, 0)] | |
def dfs(i, j): | |
A[i][j] = 2 | |
for d in dirs: | |
x, y = i + d[0], j + d[1] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Solution { | |
private int[][] dirs = {{-1, 0}, {0, -1}, {1, 0}, {0, 1}}; | |
private int r = 0, c = 0; | |
Queue<int[]> q = new ArrayDeque<>(); | |
boolean[][] visited; | |
private void dfs(int[][] A, int i, int j) { | |
A[i][j] = 2; | |
for (int[] dir : dirs) { | |
int ni = i + dir[0]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Solution { | |
public boolean validMountainArray(int[] A) { | |
if (A.length < 3 || A[0] >= A[1]) return false; | |
int i = 0; | |
while (i + 1 < A.length && A[i] < A[i + 1]) ++i; | |
if (i + 1 == A.length || A[i] == A[i + 1]) return false; | |
while (i + 1 < A.length && A[i] > A[i + 1]) ++i; | |
if (i + 1 == A.length) return true; | |
return false; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Solution: | |
def validMountainArray(self, A: List[int]) -> bool: | |
if len(A) < 3 or A[0] >= A[1]: return False | |
i = 0 | |
while i + 1 < len(A) and A[i] < A[i + 1]: i += 1 | |
if i + 1 == len(A) or A[i] == A[i + 1]: return False | |
while i + 1 < len(A) and A[i] > A[i + 1]: i += 1 | |
if i + 1 == len(A): return True | |
return False |
NewerOlder