Skip to content

Instantly share code, notes, and snippets.

View shixiaoyu's full-sized avatar
💭
Hello World :)

Xiaoyu shixiaoyu

💭
Hello World :)
View GitHub Profile
public class Block {
public int x;
public int y;
public Block(int x, int y) {
this.x = x;
this.y = y;
}
}
// This is a DFS solution, basically just like detecting if a graph has loops.
// Used a lookup table prune, with lookup, this is O(V + E), same as BFS since each node is visited once and only once
public boolean canFinish(int numCourses, int[][] prerequisites) {
if (numCourses <= 1 || prerequisites.length == 0 || prerequisites[0].length == 0) {
return true;
}
// this is adjacency list, used a set to dedup
Map<Integer, Set<Integer>> graph = new HashMap<>();
public boolean canFinishBfsTopoSort(int numCourses, int[][] prerequisites) {
if (numCourses <= 1 || prerequisites.length == 0 || prerequisites[0].length == 0) {
return true;
}
Map<Integer, Set<Integer>> graph = new HashMap<>();
// could be extracted into a build graph function
for (int i = 0; i < numCourses; i++) {
graph.put(i, new HashSet<>());
private class Node {
public int val;
public List<Node> neighbors;
public Node(int val) {
this.val = val;
this.neighbors = new ArrayList<>();
}
}
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.*;
public class Solution {
public static void main(String[] args) {
Scanner s = new Scanner(new BufferedReader(new InputStreamReader(System.in)));
int testCases = s.nextInt();
int caseNum = 1;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Scanner;
import java.util.Stack;
public class Solution {
public static void main(String[] args) {
Scanner s = new Scanner(new BufferedReader(new InputStreamReader(System.in)));
int testCases = s.nextInt();
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.*;
public class Solution {
public static void main(String[] args) {
Scanner s = new Scanner(new BufferedReader(new InputStreamReader(System.in)));
int testCases = s.nextInt();
public static void main(String[] args) {
Scanner s = new Scanner(new BufferedReader(new InputStreamReader(System.in)));
int testCases = s.nextInt();
int caseNum = 1;
Training training = new Training();
while (caseNum <= testCases) {
int n = s.nextInt();
int p = s.nextInt();
public static void main(String[] args) {
Scanner s = new Scanner(new BufferedReader(new InputStreamReader(System.in)));
int testCases = s.nextInt();
int caseNum = 1;
Plates plates = new Plates();
while (caseNum <= testCases) {
int n = s.nextInt();
int k = s.nextInt();
public void sortColors(int[] A) {
if (A == null || A.length == 0) return;
int left = 0;
int right = A.length - 1;
int i = 0;
while (i <= right) { // has to be <=, since cur value still needs to be evaluated if swapped back
if (A[i] == 1) {
i++;