This file contains hidden or 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
public class Block { | |
public int x; | |
public int y; | |
public Block(int x, int y) { | |
this.x = x; | |
this.y = y; | |
} | |
} |
This file contains hidden or 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
// 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<>(); |
This file contains hidden or 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
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<>()); |
This file contains hidden or 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
private class Node { | |
public int val; | |
public List<Node> neighbors; | |
public Node(int val) { | |
this.val = val; | |
this.neighbors = new ArrayList<>(); | |
} | |
} |
This file contains hidden or 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.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; |
This file contains hidden or 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.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(); |
This file contains hidden or 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.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(); |
This file contains hidden or 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
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(); |
This file contains hidden or 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
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(); |
This file contains hidden or 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
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++; |