BFS
文章平均质量分 71
yuanhisn
这个作者很懒,什么都没留下…
展开
-
LeetCode 127 - Word Ladder
Given two words (start and end), and a dictionary, find the length of shortest transformation sequence from start to end, such that: Only one letter can be changed at a time Each intermediate wor...原创 2015-03-02 06:51:01 · 43 阅读 · 0 评论 -
Google Interview - Perimeter of region that has the same color of that point
given grid of colors, coordinate of a point and its color, find the perimeter of the region that has the same color of that point. BFS或DFS,构成perimeter的条件是只要上下左右有一个不是同颜色或者是out of bound 用一个set记录visit的...原创 2015-07-29 09:48:00 · 46 阅读 · 0 评论 -
LeetCode 210 - Course Schedule II
There are a total of n courses you have to take, labeled from 0 to n - 1. Some courses may have prerequisites, for example to take course 0 you have to first take course 1, which is expressed as a p...原创 2015-05-26 14:09:27 · 48 阅读 · 0 评论 -
Zenefits Interview - Minimum distance to guarded rooms
求每个元素到guaraded room的最短距离 0: closed room 1: open room 2: guarded room 例如input 1 2 1 1 0 1 1 2 1 那么output是 1 0 1. 2 -1 2 1 0 1 private static int[][] dir = {{0, -1}, {0, 1}, {1, 0}, {-...原创 2015-05-08 22:51:19 · 45 阅读 · 0 评论 -
Find if there is a path between two vertices in a directed graph
Given a Directed Graph and two vertices in it, check whether there is a path from the first given vertex to second. For example, in the following graph, there is a path from vertex 1 to 3. As anothe...原创 2015-05-02 23:00:02 · 109 阅读 · 0 评论 -
Pocket Gems 面经题 - Monkey Grid Problem
There is a monkey which can walk around on a planar grid. The monkey can move one space at a time left, right, up or down. That is, from (x, y) the monkey can go to (x+1, y), (x-1, y), (x, y+1), and...原创 2015-04-25 13:52:50 · 61 阅读 · 0 评论 -
LeetCode 130 - Surrounded Regions
Given a 2D board containing 'X' and 'O', capture all regions surrounded by 'X'. A region is captured by flipping all 'O's into 'X's in that surrounded region. For example, X X X X X O O X X X O...原创 2015-03-09 10:43:22 · 66 阅读 · 0 评论 -
LeetCode 126 - Word Ladder II
Given two words (start and end), and a dictionary, find all shortest transformation sequence(s) from start to end, such that: Only one letter can be changed at a time Each intermediate word must ...原创 2015-03-02 08:12:17 · 45 阅读 · 0 评论 -
Google Interview - 警察到房间的最短距离
一个 n x n 矩阵,每个房间可能是封闭的房间,可能是警察,可能是开的房间, 封闭的房间不能过,返回一个n x n矩阵,每一个元素是最近的警察到这个房间的最短距离。 初始矩阵中-1代表封闭房间,INT_MAX代表普通房间,0代表有警察的房间。 Solution: 把警察都找出来,然后一起push到BFS的queue里面,同时搜索。复杂度可降为O(n^2)。 Starting fro...原创 2015-08-01 12:58:22 · 57 阅读 · 0 评论