Skip to content

Instantly share code, notes, and snippets.

View wushbin's full-sized avatar

Shengbin Wu wushbin

  • San Francisco Bay Area
View GitHub Profile
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
class Solution {
public List<Integer> countSmaller(int[] nums) {
List<Integer> result = new ArrayList<>();
if (nums == null || nums.length == 0) {
return result;
}
int len = nums.length;
int[] indexes = new int[len];
Integer[] res = new Integer[len];
int[] temp = new int[len];
class Solution {
public String alienOrder(String[] words) {
boolean[][] adjacent = new boolean[26][26];
// -1: no exist, 0: exist, 1: visiting, 2: visited
int[] visited = new int[26];
int[] indegree = new int[26];
Arrays.fill(visited, -1);
if (!buildGraph(adjacent, visited, words)) {
class Solution {
public boolean equationsPossible(String[] equations) {
int[] parent = new int[26];
for (int i = 0; i < 26; i++) {
parent[i] = i;
}
for (String eq : equations) {
if (eq.charAt(1) == '=') {
union(parent, eq.charAt(0) - 'a', eq.charAt(3) - 'a');
}
class Solution {
public int removeCoveredIntervals(int[][] intervals) {
Arrays.sort(intervals, (a, b) -> (a[0] != b[0] ? Integer.compare(a[0], b[0]) : Integer.compare(b[0], a[0])));
int right = -1;
int count = 0;
for (int[] inter : intervals) {
if (inter[1] > right) {
count ++;
right = inter[1];
}
class NumArray {
class SegmentTreeNode {
SegmentTreeNode left;
SegmentTreeNode right;
int sum;
int start;
int end;
public SegmentTreeNode(int start, int end){
this.left = null;
class Solution {
public List<Integer> findMinHeightTrees(int n, int[][] edges) {
Set<Integer>[] graph = new Set[n];
for (int i = 0; i < n; i++) {
graph[i] = new HashSet<>();
}
for (int[] edge : edges) {
graph[edge[0]].add(edge[1]);
graph[edge[1]].add(edge[0]);
class Solution {
public int[] gardenNoAdj(int N, int[][] paths) {
Map<Integer, Set<Integer>> graph = new HashMap<>();
for (int i = 1; i <= N; i++) {
graph.put(i, new HashSet<>());
}
for (int[] path : paths) {
graph.get(path[0]).add(path[1]);
graph.get(path[1]).add(path[0]);
}
class Solution {
public int[] findOrder(int numCourses, int[][] prerequisites) {
List<Integer>[] map = new List[numCourses];
int[] degree = new int[numCourses];
for (int i = 0; i < numCourses; i++) {
map[i] = new ArrayList<Integer>();
}
for (int[] pre : prerequisites) {
map[pre[1]].add(pre[0]);
class Solution {
public int findCheapestPrice(int n, int[][] flights, int src, int dst, int K) {
Map<Integer, Map<Integer, Integer>> graph = new HashMap<>();
for (int[] flight : flights) {
if (!graph.containsKey(flight[0])) {
graph.put(flight[0], new HashMap<>());
}
graph.get(flight[0]).put(flight[1], flight[2]);
}