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;
for (int i=0; i<m; i++) {
for (int j=0; j<n; j++) {
if (board[i][j] != 1) continue;
int area = 0;
area = dfs(board, i, j, m, n);
maxArea = Math.max(maxArea, area);
}
}
return maxArea;
}
public int dfs(int[][] board, int i, int j, int m, int n) {
if (i<0 || i>=m || j<0 || j>=n || board[i][j]!=1) return 0;
int area = 1;
board[i][j] = 2;
area += dfs(board, i-1, j, m, n);
area += dfs(board, i+1, j, m, n);
area += dfs(board, i, j-1, m, n);
area += dfs(board, i, j+1, m, n);
return area;
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
LargestIsland sol = new LargestIsland();
//int[][] arr = new int[][]{{1,0,0,0},{0,1,0,0},{0,0,1,1},{0,0,1,1}};
int[][] arr = new int[][]{{1,0,1},{0,1,0},{0,1,1}};
int res = sol.findLargestIsland(arr);
System.out.println(res);
}
}
FB面经 Prepare: Largest Island的更多相关文章
- FB面经 Prepare: Count Unique Island
数unique island, 比如 110000 110001 001101 101100 100000 总共两个unique岛,不是四个 方法可以是记录每次新的岛屿搜索的路径,left,right ...
- FB面经 Prepare: All Palindromic Substrings
Given a string, calculate how many substring is palindrome. Ignore non-char characters. Ignore case; ...
- FB面经 Prepare: Task Schedule
tasks has cooldown time, give an input task id array, output finish time input: AABCA A--ABCA output ...
- FB面经 Prepare: Make Parentheses valid
给一组括号,remove最少的括号使得它valid 从左从右各scan一次 package fb; public class removeParen { public static String fi ...
- FB面经Prepare: Friends Recommendation
有个getFriend() API, 让你推荐你的朋友的朋友做你的朋友,当然这个新朋友不能是你原来的老朋友 package fb; import java.util.*; public class R ...
- FB面经Prepare: Dot Product
Conduct Dot Product of two large Vectors 1. two pointers 2. hashmap 3. 如果没有额外空间,如果一个很大,一个很小,适合scan小的 ...
- FB面经prepare: Count the number of Vector
给一个超级大的排好序的vector [abbcccdddeeee]比如,要求返回[{,a}, {,b}, {,c}, {,d}, {,e}......]复杂度要优于O(N) 分析: 如果是binary ...
- FB面经prepare: task schedule II
followup是tasks是无序的. 一开始是有序的,比如说1, 1, 2, 1,一定要先执行第一个task1,然后等task1恢复,再执行第2个task1,再执行task2..... follow ...
- FB面经prepare: Task Schedule
每种task都有冷却时间,比如task1执行后,要经过interval时间后才能再次执行,求总共所需时间. 用HashMap保存每一个task的下一次可以开始执行的最早时间 package TaskS ...
随机推荐
- python面试题整理
1.谈谈你对csrf的理解和django中CSRF防护机制. 什么是 CSRF CSRF, Cross Site Request Forgery, 跨站点伪造请求.举例来讲,某个恶意的网站上有一个指向 ...
- react-native清除android项目缓存的命令
cd到android目录下执行: ./gradlew clean
- Linux awk学习
零.awk标准语法 [root@wohaoshuai1 bbb]# echo "abcd" |awk 'BEGIN{print "wohaoshuai"} /a ...
- IDEA安装ini4idea插件
参见https://blog.csdn.net/lintianlin/article/details/80050309
- POJ 1149 PIGS 【最大流】
<题目链接> 题目大意:有一个养猪场,厂长没有钥匙,这个养猪场一共M个猪圈,N个顾客,每个顾客有一些猪圈的钥匙,每个顾客需要一些猪,问你厂长最多能卖多少猪?这里有个条件是,厂长可以在一个顾 ...
- Go语言基础(一)
Go语言基础(一) 国庆体验一下大名鼎鼎的Go语言,IDE使用IEDA+Go插件,边敲代码边体会,感觉Go语言好酷 一.Hello World 和Java类似,go文件需要一个package包含,代码 ...
- (18)0907_CSS选择器详解
选择器的优先级(决定那个样式生效): important: > 内联样式 > id选择器> 类和伪类 > 元素选择器 > 通配选择器> 继承样式无优先级 最大 ...
- Appium-Python-Windows环境搭建笔记
Appium版本:1.11.0 操作系统:Windows7-64位 开发语言:Python 3.7.2 测试应用平台:安卓 5.1.1 Appium服务端 一.JDK 也许你会觉得很奇怪,我搭建Pyt ...
- PAT甲级1091 Acute Stroke【三维bfs】
题目:https://pintia.cn/problem-sets/994805342720868352/problems/994805375457411072 题意: 求三维的连通块 思路: 简单b ...
- Java中如何使用非强制类型转换把字符串转换成int类型
①强制类型转换代码如下: String string = "123456"; int a,b = 0; @Test public void String2Int1() { //方法 ...