Skip to content

Instantly share code, notes, and snippets.

@Desolve
Desolve / 0802 Find Eventual Safe States.py
Created June 5, 2023 15:03
0802 Find Eventual Safe States
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
@Desolve
Desolve / 0802 Find Eventual Safe States.java
Created June 5, 2023 15:02
0802 Find Eventual Safe States
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);
@Desolve
Desolve / 0547 Number of Provinces.py
Created May 31, 2023 14:10
0547 Number of Provinces
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]]
@Desolve
Desolve / 0547 Number of Provinces.java
Created May 31, 2023 14:10
0547 Number of Provinces
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];
@Desolve
Desolve / 0797 All Paths From Source to Target.py
Created May 30, 2023 14:44
0797 All Paths From Source to Target
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
@Desolve
Desolve / 0797 All Paths From Source to Target.java
Created May 30, 2023 14:44
0797 All Paths From Source to Target
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));
@Desolve
Desolve / 0934 Shortest Bridge.py
Created May 28, 2023 16:00
0934 Shortest Bridge
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]
@Desolve
Desolve / 0934 Shortest Bridge.java
Created May 28, 2023 15:59
0934 Shortest Bridge
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];
@Desolve
Desolve / 0941 Valid Mountain Array.java
Created January 30, 2022 09:15
0941 Valid Mountain Array
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;
@Desolve
Desolve / 0941 Valid Mountain Array.py
Created January 30, 2022 09:14
0941 Valid Mountain Array
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