
BFS
文章平均质量分 76
flyatcmu
这个作者很懒,什么都没留下…
展开
-
Shortest Path to Get All Keys
You are given anm x ngridgridwhere:'.'is an empty cell. '#'is a wall. '@'is the starting point. Lowercase letters represent keys. Uppercase letters represent locks.You start at the starting point and one move consists of walking one space in ...原创 2022-05-16 05:33:38 · 319 阅读 · 0 评论 -
Shortest Path Visiting All Nodes
You have an undirected, connected graph ofnnodes labeled from0ton - 1. You are given an arraygraphwheregraph[i]is a list of all the nodes connected with nodeiby an edge.Returnthe length of the shortest path that visits every node. You may sta...原创 2022-05-16 03:05:02 · 219 阅读 · 0 评论 -
Shortest Path to Get Food
You are starving and you want to eat food as quickly as possible. You want to find the shortest path to arrive at any food cell.You are given anm x ncharacter matrix,grid, of these different types of cells:'*'is your location. There isexactly one...原创 2022-05-06 14:46:26 · 142 阅读 · 0 评论 -
K-Similar Strings
Stringss1ands2arek-similar(for some non-negative integerk) if we can swap the positions of two letters ins1exactlyktimes so that the resulting string equalss2.Given two anagramss1ands2, return the smallestkfor whichs1ands2arek-simil...原创 2022-04-20 14:08:48 · 181 阅读 · 0 评论 -
Shortest Path to Get Food
You are starving and you want to eat food as quickly as possible. You want to find the shortest path to arrive at any food cell.You are given anm x ncharacter matrix,grid, of these different types of cells:'*'is your location. There isexactly one...原创 2022-04-09 02:05:18 · 232 阅读 · 0 评论 -
Minimum Moves to Move a Box to Their Target Location
A storekeeper is a game in which the player pushes boxes around in a warehouse trying to get them to target locations.The game is represented by anm x ngrid of charactersgridwhere each element is a wall, floor, or box.Your task is to move the box'...原创 2022-03-28 10:54:01 · 447 阅读 · 0 评论 -
Minimum Knight Moves
In aninfinitechess board with coordinates from-infinityto+infinity, you have aknightat square[0, 0].A knight has 8 possible moves it can make, as illustrated below. Each move is two squares in a cardinal direction, then one square in an orthogona...原创 2022-03-11 03:21:39 · 372 阅读 · 0 评论 -
Lexicographically Smallest String After Applying Operations
You are given a stringsofeven lengthconsisting of digits from0to9, and two integersaandb.You can apply either of the following two operations any number of times and in any order ons:Addato all odd indices ofs(0-indexed). Digits post9a...原创 2020-10-18 12:42:24 · 492 阅读 · 2 评论 -
Frog Position After T Seconds
Given an undirected treeconsisting ofnvertices numbered from 1 ton. A frog starts jumpingfrom thevertex 1. In one second, the frogjumps from itscurrentvertex to anotherunvisitedvertex if they are directly connected. The frog can not jump back to...原创 2020-09-07 11:20:23 · 202 阅读 · 0 评论 -
Minimum Number of Days to Eat N Oranges
There arenoranges in the kitchen and you decided to eat some of these oranges every day as follows:Eat one orange. If the number of remaining oranges (n) is divisible by 2 then you can eat n/2 oranges. If the number of remaining oranges (n) is divis...原创 2020-08-16 13:34:40 · 252 阅读 · 0 评论 -
Is Graph Bipartite
Given an undirectedgraph, returntrueif and only if it is bipartite.Recall that a graph isbipartiteif we can split it's set of nodes into two independentsubsets A and B such that every edge in ...原创 2020-02-21 15:24:03 · 269 阅读 · 0 评论 -
Rotting Oranges
In a given grid, each cell can have one of threevalues:the value0representing an empty cell; the value1representing a fresh orange; the value2representing a rotten orange.Every minute, any...原创 2020-03-22 08:55:16 · 418 阅读 · 0 评论 -
BFS 到 Level Order traverse 到 UnionFind 到 Topological Sort 到 Dijkstra 思路总结
BFSLevel OrderTopological SortDijkstra原创 2020-07-01 09:44:21 · 975 阅读 · 0 评论 -
Employee Importance
You are given a data structure of employee information, which includes the employee'sunique id, hisimportance valueand hisdirectsubordinates' id.For example, employee 1 is the leader of employee 2, and employee 2 is the leader of employee 3. They ha...原创 2020-06-29 03:09:07 · 162 阅读 · 0 评论 -
Race Car
Your car starts at position 0 and speed +1 on an infinite number line. (Your car can go into negative positions.)Your car drives automatically according to a sequence of instructions A (accelerate) and R (reverse).When you get an instruction "A", your.原创 2020-06-25 13:11:18 · 244 阅读 · 0 评论 -
Jump Game IV
Given an array ofintegersarr, you are initially positioned at the first index of the array.In one step you can jump from indexito index:i + 1where:i + 1 < arr.length. i - 1where:i - 1 >= 0. jwhere:arr[i] == arr[j]andi != j.Returnt...原创 2020-06-22 12:03:45 · 399 阅读 · 0 评论 -
Shortest Bridge
In a given 2D binary arrayA, there are two islands. (An island is a 4-directionally connected group of1s not connected to any other 1s.)Now, we may change0s to1s so as to connect the two islands together to form 1 island.Return the smallest number...原创 2020-06-07 08:08:00 · 213 阅读 · 0 评论 -
Possible Bipartition
Given a set ofNpeople (numbered1, 2, ..., N), we would like to split everyone into two groups ofanysize.Each person may dislike some other people, and they should not go into the same group.Formally, ifdislikes[i] = [a, b], it means it is not al...原创 2020-06-07 06:19:11 · 151 阅读 · 0 评论 -
Reorder Routes to Make All Paths Lead to the City Zero
There arencities numbered from0ton-1andn-1roads such thatthere is only one way to travel between twodifferent cities (this network form a tree).Last year,The ministry of transportdecided to orient the roads in one direction because they are to...原创 2020-05-31 13:35:46 · 246 阅读 · 0 评论 -
Shortest Path in Binary Matrix
In an N by N square grid, each cell is either empty (0) or blocked (1).Aclearpath from top-left to bottom-righthas lengthkif and only if it is composed of cellsC_1, C_2, ..., C_ksuch that:Adjacent cellsC_iandC_{i+1}are connected 8-directiona...原创 2020-05-30 04:52:51 · 211 阅读 · 0 评论 -
Maximum Width of Binary Tree
Given a binary tree, write a function to get the maximum width of the given tree. The width of a tree is the maximum width among all levels. The binary tree has the same structure as afull binary tree, but some nodes are null.The width of one level is d.原创 2020-05-29 14:39:14 · 177 阅读 · 0 评论 -
Check Completeness of a Binary Tree
Given a binary tree, determine if it is acomplete binary tree.Definition of a complete binary tree fromWikipedia:In a complete binary tree every level, except possibly the last, is completely filled, and all nodes in the last level are as far left as ..原创 2020-05-27 12:11:24 · 142 阅读 · 0 评论 -
Battleships in a Board
Given an 2D board, count how many battleships are in it. The battleships are represented with'X's, empty slots are represented with'.'s. You may assume the following rules:You receive a valid board, made of only battleships or empty slots. Battleships..原创 2020-05-12 12:44:09 · 214 阅读 · 0 评论 -
Island Perimeter
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 island (i原创 2020-05-10 06:55:57 · 179 阅读 · 0 评论 -
Flood Fill
Animageis represented by a 2-D array of integers, each integer representing the pixel value of the image (from 0 to 65535).Given a coordinate(sr, sc)representing the starting pixel (row and colu...原创 2020-05-04 12:42:08 · 178 阅读 · 0 评论 -
Max Area of Island
Given a non-empty 2D arraygridof 0's and 1's, anislandis a group of1's (representing land) connected 4-directionally (horizontal or vertical.) You may assume all four edges of the grid are surrou...原创 2020-05-03 07:16:14 · 207 阅读 · 0 评论 -
Serialize and Deserialize N-ary Tree
Serialization is the process of converting a data structure or object into a sequence of bits so that it can be stored in a file or memory buffer, or transmitted across a network connection link to be...原创 2020-04-23 10:32:28 · 373 阅读 · 0 评论 -
All Nodes Distance K in Binary Tree
We are given a binary tree (with root noderoot), atargetnode, and an integer valueK.Return a list of the values of allnodes that have a distanceKfrom thetargetnode. The answer can be retur...原创 2020-04-07 12:24:10 · 309 阅读 · 0 评论 -
Bus Routes
We have a list of bus routes. Eachroutes[i]is a bus route that the i-th busrepeats forever. For example ifroutes[0] = [1, 5, 7], this means that the firstbus (0-th indexed) travels in the sequenc...原创 2020-04-04 13:58:31 · 529 阅读 · 0 评论 -
Get Watched Videos by Your Friends
There arenpeople, each person has a uniqueidbetween0andn-1. Given the arrayswatchedVideosandfriends, wherewatchedVideos[i]andfriends[i]contain the list of watched videos and the list of ...原创 2020-04-04 12:46:24 · 2310 阅读 · 0 评论 -
Pacific Atlantic Water Flow
Given anm x nmatrix of non-negative integers representing the height of each unit cell in a continent, the "Pacific ocean" touches the left and top edges of the matrix and the "Atlantic ocean" touch...原创 2020-04-04 11:01:37 · 169 阅读 · 0 评论 -
Open the Lock
You have a lock in front of you with 4 circular wheels. Each wheel has 10 slots:'0', '1', '2', '3', '4', '5', '6', '7', '8', '9'. The wheels can rotate freely and wrap around: for example we can turn...原创 2020-04-03 05:21:31 · 346 阅读 · 0 评论 -
Minimum Number of Flips to Convert Binary Matrix to Zero Matrix
Given am x nbinary matrixmat. In one step, you can choose one cell and flip it and all the four neighbours of itif they exist (Flip is changing 1 to 0 and 0 to 1). A pair of cells are called neigh...原创 2020-04-03 04:25:48 · 259 阅读 · 0 评论 -
Shortest Distance from All Buildings
You want to build a house on anemptyland which reaches all buildings in the shortest amount of distance. You can only move up, down, left and right. You are given a 2D grid of values0,1or2, wher...原创 2020-04-03 02:13:05 · 225 阅读 · 0 评论 -
Sliding Puzzle
On a 2x3board, there are 5 tiles represented by the integers 1 through 5, and an empty square represented by 0.A move consists of choosing0and a 4-directionally adjacent number and swapping it....原创 2020-03-23 08:37:21 · 532 阅读 · 0 评论 -
Closest Leaf in a Binary Tree
Input:root = [1,2,3,4,null,null,null,5,null,6], k = 2Diagram of binary tree: 1 / \ 2 3 / 4 / 5 / 6Output: 3Expl...原创 2020-02-20 16:00:11 · 156 阅读 · 0 评论 -
The Maze II
Input 1: a maze represented by a 2D array0 0 1 0 00 0 0 0 00 0 0 1 01 1 0 1 10 0 0 0 0Input 2: start coordinate (rowStart, colStart) = (0, 4)Input 3: destination coordinate (rowDest, colDest...原创 2020-02-18 05:12:57 · 257 阅读 · 0 评论 -
Minimum Height Trees
For an undirected graph with tree characteristics, we can choose any node as the root. The result graph is then a rooted tree. Among all possible rooted trees, those with minimum height are called min...原创 2020-02-17 14:36:05 · 186 阅读 · 0 评论 -
Web Crawler
Implement a webpage Crawler to crawl webpages ofhttp://www.wikipedia.org/. To simplify the question, let's use url instead of the the webpage content.Your crawler should:CallHtmlHelper.parseUrls...原创 2020-02-17 11:35:18 · 550 阅读 · 0 评论 -
Maximum Depth of N-ary Tree
Input: root = [1,null,2,3,4,5,null,null,6,7,null,8,null,9,10,null,null,11,null,12,null,13,null,null,14]Output: 5思路:BFS level order/*// Definition for a Node.class Node { public int val...原创 2020-02-16 06:05:29 · 185 阅读 · 0 评论