FB面经 Prepare: Count Unique Island】的更多相关文章

数unique island, 比如 110000 110001 001101 101100 100000 总共两个unique岛,不是四个 方法可以是记录每次新的岛屿搜索的路径,left,right,up,down, 作为标志是否相同的key,存hashset package fbOnsite; import java.util.*; public class UniqueIsland { public int countIsland(int[][] grid) { HashSet<Strin…
给一个超级大的排好序的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…
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;…
Is there a method in Ruby that takes an array, and counts all unique elements and their occurrences and passes them back as a hash? For example ['A','A','A','A','B','B','C'].method > {'A' => 4, 'B' => 2, 'C' => 1} Something like that. ['A','A'…
有个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…
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…
有一些账号,账号里面有一个或多个email, 如果两个账号有共同的email,则认为这两个账号是同一个人,找出哪些账号是同一个人 输入是这样的:数字是用户,字母是邮箱,有很多人有多个邮箱,找出相同的用户 1- {x,y,z} 2-{x} 3-{a,b} 4-{y, z} 5-{b} 6-{m} 7-{t,b} 账号之间如果有公共email就连边,我觉得要用hashMap, 存(email, user) pair 如果当前user的某个email在hashMap里出现过,当时是user1, 就看u…
给一组括号,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…
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…