Skip to content

Instantly share code, notes, and snippets.

View tolinwei's full-sized avatar

Wei Lin tolinwei

  • Facebook
  • Cambridge, MA
View GitHub Profile
@tolinwei
tolinwei / KnightDialer.java
Created December 4, 2021 16:12
Knight Dialer
class Solution {
int mod = (int) Math.pow(10, 9) + 7;
int[][] directions = new int[][] {
{-2, 1},
{-2, -1},
{2, -1},
{2, 1},
{-1, 2},
{-1, -2},
{1, 2},
@tolinwei
tolinwei / AlienDictionary.java
Last active November 15, 2021 03:54
Alien Dictionary
public String alienOrder(String[] words) {
if (words == null || words.length == 0) {
return "";
}
// adjacency list
Map<Character, List<Character>> graph = new HashMap<>();
// for topology sort
Map<Character, Integer> inDegree = new HashMap<>();
// WRONG 2: still populate the graph and inDegree, or NLE in the topology sort later
@tolinwei
tolinwei / SearchInRotatedArray.java
Created November 15, 2021 03:21
Search in rotated array
class Solution {
public int search(int[] nums, int target) {
if (nums == null || nums.length == 0) {
return -1;
}
// find pivot
int left = 0;
int right = nums.length - 1;
while (left < right) {
@tolinwei
tolinwei / TaskScheduler.java
Created November 2, 2021 02:52
Task Scheduler
public int leastInterval(char[] tasks, int n) {
int[] counter = new int[26];
int max = 0; // 最多的字母的频数
int maxCount = 0; // 有几个这样子的字母
for(char task : tasks) {
counter[task - 'A']++;
if(max == counter[task - 'A']) {
maxCount++;
}
else if(max < counter[task - 'A']) {
@tolinwei
tolinwei / AccountMerge.java
Created November 2, 2021 02:37
Account Merge
public List<List<String>> accountsMerge(List<List<String>> accounts) {
if (accounts == null || accounts.size() == 0) {
return new ArrayList<>();
}
UnionFind uf = new UnionFind(10000);
Map<String, String> emailName = new HashMap<>();
Map<String, Integer> emailId = new HashMap<>();
Map<Integer, List<String>> tempRes = new HashMap<>();
int index = 0;
@tolinwei
tolinwei / UnionFind.java
Created November 2, 2021 02:24
Union & Find
class UnionFind {
int[] parent;
int[] rank;
public UnionFind(int n) {
parent = new int[n];
rank = new int[n];
for (int i = 0; i < n; i++) {
parent[i] = i;
}
}
@tolinwei
tolinwei / gist:27010332f520d2841e7f4df9e6e91d42
Created October 29, 2021 02:28
All nodes distance K binary tree
public List<Integer> distanceK(TreeNode root, TreeNode target, int K) {
Map<Integer, Integer> map = new HashMap<>(); // distance to target from root
find(root, target, map);
System.out.println("Map");
for (Map.Entry<Integer, Integer> entry : map.entrySet()) {
System.out.println(entry.getKey() + " ->" + entry.getValue());
}
List<Integer> res = new ArrayList<>();
helper(root, target, K, map.get(root.val), map, res);
public void flatten(TreeNode root) {
while (root != null) {
if (root.left != null) {
// find the right most in left subtree
TreeNode rightMostInLeft = root.left;
while (rightMostInLeft.right != null) {
rightMostInLeft = rightMostInLeft.right;
}
rightMostInLeft.right = root.right;
root.right = root.left;
{
"slots": [
"SportsTeam"
],
"prioritizedAuthorities": [
{
"rankedAuthorities": [
"LIVE_TV_SPORTS_TEAM_ER_SERVICE",
"LIVE_SPORTS_ER_SERVICE"
]
import java.util.*;
public class Main {
public static void main(String args[])
{
Map<Integer, Integer> map = new HashMap();
map.put(1, 3);
map.put(2, 4);
map.put(5, 7);