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

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;…
数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…
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…
followup是tasks是无序的. 一开始是有序的,比如说1, 1, 2, 1,一定要先执行第一个task1,然后等task1恢复,再执行第2个task1,再执行task2..... followup是无序的,就是不用按给的顺序执行,也就是可以先执行task1,然后task1还没恢复时,先执行task2, etc...... 正确的做法应该是统计每个task的frequency,然后每次选frequency最高并且可以执行的task执行. 用maxHeap存每个task的剩余frequenc…
每种task都有冷却时间,比如task1执行后,要经过interval时间后才能再次执行,求总共所需时间. 用HashMap保存每一个task的下一次可以开始执行的最早时间 package TaskSchedule; import java.util.*; public class Solution { public int schedule(int[] str, int recover) { if (str==null || str.length==0) return 0; if (recov…
有一些账号,账号里面有一个或多个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…
Merge K sorted Array 跟Merge K sorted lists不同在于,从PQ里poll出来以后不知道下一个需要被加入PQ的是哪一个 所以需要写一个wrapper class package fbPractise; import java.util.*; public class MergeKLists { static class Pair { int listIndex; int idInList; int value; public Pair(int l, int i…
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…
给一个 二叉树 , 求最深节点的最小公共父节点 . retrun . 先用 recursive , 很快写出来了, 要求用 iterative . 时间不够了... Recursion: 返回的时候返回lca和depth,每个node如果有大于一个子节点的depth相同就返回这个node,如果有一个子节点depth更深就返回个子节点lca,这个o(n)就可以了 Iteration: tree的recursion换成iteration处理,一般用stack都能解决吧(相当于手动用stack模拟re…
Give n points on 2-D plane, find the K closest points to origin Based on bucket sort: package fbPractise; import java.util.*; class Coordinate { int x; int y; public Coordinate(int x, int y) { this.x = x; this.y = y; } } public class Kclosest { publi…
Given a num array, find a window of size k, that has a maximum sum of the k entries. follow-up: Find three non-overlap windows of size k, that together has a maximum sum of the 3k entries, time complexity O(n^2) Follow Up: 这个其实可以优化到O(n)时间.建从左端到每个下标的最…
给的多叉树, 找这颗树里面最长的路径长度 解法就是在子树里面找最大的两个(或一个,如果只有一个子树的话)高度加起来. 对于每一个treenode, 维护它的最高的高度和第二高的高度,经过该点的最大路径就是:  最高高度+第二高高度,然后return 最高高度 package fbPractise; import java.util.*; class TreeNode { int val; List<TreeNode> children; public TreeNode(int value) {…
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…
字符串重新排列,让里面不能有相同字母在一起.比如aaabbb非法的,要让它变成ababab.给一种即可 Greedy: 跟FB面经Prepare task Schedule II很像,记录每个char出现次数,然后用最大堆,把剩下char里面出现次数多的优先Poll出来组建新的string 如果poll出来的char跟上一个相同,则用一个queue暂时存一下 我觉得时间复杂度:O(N) + O(KlogK) + O(NlogK) = O(NlogK) ,where K is the number…
In a 2D grid of 0s and 1s, we change at most one 0 to a 1. After, what is the size of the largest island? (An island is a 4-directionally connected group of 1s). Example 1: Input: [[1, 0], [0, 1]] Output: 3 Explanation: Change one 0 to 1 and connect…
In a 2D grid of 0s and 1s, we change at most one 0 to a 1. After, what is the size of the largest island? (An island is a 4-directionally connected group of 1s). Example 1: Input: [[1, 0], [0, 1]] Output: 3 Explanation: Change one 0 to 1 and connect…
In a 2D grid of 0s and 1s, we change at most one 0 to a 1. After, what is the size of the largest island? (An island is a 4-directionally connected group of 1s). Example 1: Input: [[1, 0], [0, 1]] Output: 3 Explanation: Change one 0 to 1 and connect…
n<=100000个山,每个山有高度,从一个山到另一个山代价为高度差,有A和B两人一起开车,A每次选前进方向的次近山,B选最近,保证山高度不同且如果代价相同的山低的代价算小,每次旅行先A走,然后B,然后AB轮流开车,旅行如果下一次找不到目的地或者下一次到目的地时总代价超过了指定的X,他们就会停下.现完成两个任务:一,告诉X0,问从哪个点开始完成一次预算代价为X0的旅行会使A的路程比B的路程最小:二,m个询问,每次问从Si做预算Xi的旅行,A和B的行驶路程. 首先需要知道每个人后面的最近和次近山,…
Given a list of non negative integers, arrange them such that they form the largest number. For example, given [3, 30, 34, 5, 9], the largest formed number is 9534330. Note: The result may be very large, so you need to return a string instead of an i…
什么时候需要 Looper Looper用于封装了android线程中的消息循环,默认情况下一个线程是不存在消息循环(message loop)的,需要调用Looper.prepare()来给线程创建一个消息循环,调用Looper.loop()来使消息循环起作用,使用Looper.prepare()和Looper.loop()创建了消息队列就可以让消息处理在该线程中完成. 使用Looper需要注意什么 写在Looper.loop()之后的代码不会被立即执行,当调用后mHandler.getLoo…
You are given a map in form of a two-dimensional integer grid where 1 represents land and 0 represents water. Grid cells are connected horizontally/vertically (not diagonally). The grid is completely surrounded by water, and there is exactly one isla…
Given an array which consists of non-negative integers and an integer m, you can split the array into m non-empty continuous subarrays. Write an algorithm to minimize the largest sum among these m subarrays. Note:Given m satisfies the following const…
Given a set of distinct positive integers, find the largest subset such that every pair (Si, Sj) of elements in this subset satisfies: Si % Sj = 0 or Sj % Si = 0. If there are multiple solutions, return any subset is fine. Example 1: nums: [1,2,3] Re…
Given a binary tree, find the largest subtree which is a Binary Search Tree (BST), where largest means subtree with largest number of nodes in it. Note:A subtree must include all of its descendants.Here's an example: 10 / \ 15 / \ \ 1 8 7 The Largest…
Find the kth largest element in an unsorted array. Note that it is the kth largest element in the sorted order, not the kth distinct element. For example, Given [3,2,1,5,6,4] and k = 2, return 5. Note: You may assume k is always valid, 1 ≤ k ≤ array'…