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
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}, |
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 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 |
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
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) { |
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 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']) { |
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 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; |
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
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; | |
} | |
} |
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 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); |
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 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; |
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
{ | |
"slots": [ | |
"SportsTeam" | |
], | |
"prioritizedAuthorities": [ | |
{ | |
"rankedAuthorities": [ | |
"LIVE_TV_SPORTS_TEAM_ER_SERVICE", | |
"LIVE_SPORTS_ER_SERVICE" | |
] |
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.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); |
NewerOlder