FB面经Prepare: Bipartite a graph】的更多相关文章

input friends relations{{1,2}, {2,3}, {3,4}} 把人分成两拨,每拨人互相不认识, 所以应该是group1{1,3}, group2{2,4} 这道题应该是how to bipartite a graph Taken from GeeksforGeeks Following is a simple algorithm to find out whether a given graph is Birpartite or not using Breadth F…
Given a string, calculate how many substring is palindrome. Ignore non-char characters. Ignore case; Ex: "A@,b123a", equals "Aba", should output 4: "A", "b", "a", "Aba" Spread from center: Like L…
tasks has cooldown time, give an input task id array, output finish time input: AABCA A--ABCA output:7 package fb; import java.util.*; public class Scheduler { public int task(int[] tasks, int cooldown) { int time = 0; HashMap<Integer, Integer> map…
给一组括号,remove最少的括号使得它valid 从左从右各scan一次 package fb; public class removeParen { public static String fix(String str) { StringBuffer res = new StringBuffer(str); int l = 0, r = 0; int i = 0; while (i < res.length()) { if (res.charAt(i) == '(') l++; else…
有个getFriend() API, 让你推荐你的朋友的朋友做你的朋友,当然这个新朋友不能是你原来的老朋友 package fb; import java.util.*; public class ReferFriends { public List<String> recommendation(String name) { List<String> res = new ArrayList<String>(); if (name==null || name.length…
Conduct Dot Product of two large Vectors 1. two pointers 2. hashmap 3. 如果没有额外空间,如果一个很大,一个很小,适合scan小的,并且在大的里面做binary search package fb; public class DotProduct { public int dotPro(int[][] v1, int[][] v2) { int[][] shortV; int[][] longV; if (v1.length…
给一个超级大的排好序的vector [abbcccdddeeee]比如,要求返回[{,a}, {,b}, {,c}, {,d}, {,e}......]复杂度要优于O(N) 分析: 如果是binary search找每个char的上下界,worst case要找n次,时间复杂度O(nlogn) 所以考虑每次比较start point和start point + 2^n位置上的数,假如一样就continue,不一样就在区间里面binary search找上界,这样worst case O(N) p…
You are given a tree (a simple connected graph with no cycles). The tree has nodes numbered from to and is rooted at node . Find the maximum number of edges you can remove from the tree to get a forest such that each connected component of the forest…
Find largest island in a board package fb; public class LargestIsland { public int findLargestIsland(int[][] board) { if (board==null || board.length==0 || board[0].length==0) return 0; int m = board.length; int n = board[0].length; int maxArea = 0;…
followup是tasks是无序的. 一开始是有序的,比如说1, 1, 2, 1,一定要先执行第一个task1,然后等task1恢复,再执行第2个task1,再执行task2..... followup是无序的,就是不用按给的顺序执行,也就是可以先执行task1,然后task1还没恢复时,先执行task2, etc...... 正确的做法应该是统计每个task的frequency,然后每次选frequency最高并且可以执行的task执行. 用maxHeap存每个task的剩余frequenc…