DFS
文章平均质量分 75
flyatcmu
这个作者很懒,什么都没留下…
展开
-
Find Longest Duplicate Path in Matrix
Given an integer matrix, find the length of the longest path that have same values. The matrix has no boundary limits. (like, Google Maps - see edit for context)From each cell, you can either move to two directions: horizontal or vertical. You may NOT原创 2022-05-09 13:01:15 · 295 阅读 · 0 评论 -
Construct Quad Tree
Given an * nmatrixgridof0'sand1'sonly. We want to represent thegridwith a Quad-Tree.Returnthe root of the Quad-Treerepresenting thegrid.Notice that you can assign the value of a node toTrueorFalsewhenisLeafisFalse, and both areacce...原创 2022-03-22 12:38:06 · 178 阅读 · 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 · 257 阅读 · 0 评论 -
Valid Arrangement of Pairs
You are given a0-indexed2D integer arraypairswherepairs[i] = [starti, endi]. An arrangement ofpairsisvalidif for every indexiwhere1 <= i < pairs.length, we haveendi-1== starti.Returnanyvalid arrangement ofpairs.Note:The inputs w...原创 2021-12-06 09:18:45 · 220 阅读 · 0 评论 -
Throne Inheritance
A kingdom consists of a king, his children, his grandchildren, and so on. Every once in a while, someone in the family dies or a child is born.The kingdom has a well-defined order of inheritance that consists of the king as the first member. Let's define原创 2020-09-27 12:48:10 · 213 阅读 · 0 评论 -
Maximum Non Negative Product in a Matrix
You are given arows x colsmatrixgrid.Initially, youare located at the top-leftcorner(0, 0),and in each step, you can onlymove rightordownin the matrix.Among all possible paths starting from the top-left corner(0, 0)and ending in the bottom-...原创 2020-09-20 13:20:39 · 259 阅读 · 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 · 174 阅读 · 0 评论 -
Number of Nodes in the Sub-Tree With the Same Label
Given a tree (i.e. a connected, undirected graph that has no cycles) consisting ofnnodes numbered from0ton - 1and exactlyn - 1edges. Therootof the tree is the node0, and each node of the tree hasa labelwhich is a lower-case character given in ...原创 2020-07-19 12:42:08 · 406 阅读 · 0 评论 -
Smallest Sufficient Team
In a project, you have a list of required skillsreq_skills,and a list ofpeople. The i-th personpeople[i]contains a list of skills that person has.Consider asufficient team: a set of people such that for every required skill inreq_skills, there is...原创 2020-06-28 04:55:50 · 269 阅读 · 0 评论 -
Bricks Falling When Hit
We have a grid of 1s and 0s; the 1s in a cell represent bricks. A brick will not drop if and only if it is directly connected to the top of the grid, or at least one of its (4-way) adjacent bricks will not drop.We will do some erasuressequentially. Eac..原创 2020-06-22 03:03:14 · 226 阅读 · 0 评论 -
Android Unlock Patterns
Given an Android3x3key lock screen and two integersmandn, where 1 ≤ m ≤ n ≤ 9, count the total number of unlock patterns of the Android lock screen, which consist of minimum ofmkeys and maximumnkeys.Rules for a valid pattern:Each pattern must ...原创 2020-06-20 11:32:05 · 200 阅读 · 0 评论 -
Most Stones Removed with Same Row or Column
On a 2D plane, we place stones at some integer coordinate points. Each coordinate point may have at most one stone.Now, amoveconsists of removing a stonethat shares a column or row with another stone on the grid.What is the largest possible number ...原创 2020-06-18 13:53:17 · 220 阅读 · 0 评论 -
Kth Ancestor of a Tree Node
You are given a tree withnnodes numbered from0ton-1in the form of a parent array whereparent[i]is the parent of nodei. The root of the tree is node0.Implement the functiongetKthAncestor(int node, int k)to return thek-th ancestor of the given...原创 2020-06-14 12:40:07 · 383 阅读 · 0 评论 -
Stone Game II
Alexand Lee continue theirgames with piles of stones. There are a number ofpilesarranged in a row, and each pile has a positive integer number of stonespiles[i]. The objective of the game is to end with the moststones.Alex and Lee take turns, wi...原创 2020-06-11 07:47:35 · 236 阅读 · 0 评论 -
[leetcode] Stone Game
Alex and Lee play a game with piles of stones. There are an even number ofpilesarranged in a row, and each pile has a positive integer number of stonespiles[i].The objective of the game is to end with the moststones. The total number of stones is o...原创 2020-06-11 03:30:17 · 323 阅读 · 0 评论 -
Unique Paths III
On a 2-dimensionalgrid, there are 4 types of squares:1represents the starting square. There is exactly one starting square. 2represents the ending square. There is exactly one ending square. 0represents empty squares we can walk over. -1represe...原创 2020-06-10 14:17:57 · 180 阅读 · 0 评论 -
Minesweeper
Let's play the minesweeper game (Wikipedia,online game)!You are given a 2D char matrix representing the game board.'M'represents anunrevealedmine,'E'represents anunrevealedempty square,'B'represents arevealedblank square that has no adjacent...原创 2020-06-10 13:19:52 · 233 阅读 · 0 评论 -
Making A Large Island
In a 2D grid of0s and1s, we change at most one0to a1.After, what is the size of the largest island?(An island is a 4-directionally connected group of1s).Example 1:Input: [[1, 0], [0, 1]]Output: 3Explanation: Change one 0 to 1 and connect tw...原创 2020-06-10 10:41:52 · 181 阅读 · 0 评论 -
Linked List in Binary Tree
Given a binary treerootand alinked list withheadas the first node.Return True if all the elements in the linked list starting from theheadcorrespond to somedownward pathconnected in the binary treeotherwise return False.In this context downw...原创 2020-06-09 12:47:03 · 220 阅读 · 0 评论 -
Print Immutable Linked List in Reverse
You are given an immutable linked list, print out all values of each node in reverse with the help of the followinginterface:ImmutableListNode:An interface of immutable linked list, you are given the head of the list.You need to use the following func..原创 2020-06-08 10:38:41 · 267 阅读 · 0 评论 -
Two City Scheduling
There are2Npeople a company is planning to interview. The cost of flying thei-th person to cityAiscosts[i][0], and the cost of flying thei-th person to cityBiscosts[i][1].Return the minimum cost to fly every person to a city such that exactlyN...原创 2020-06-08 08:45:50 · 246 阅读 · 0 评论 -
Construct Binary Tree from String
You need to construct a binary tree from a string consisting of parenthesis and integers.The whole input represents a binary tree. It contains an integer followed by zero, one or two pairs of parenthesis. The integer represents the root's value and a pai原创 2020-06-08 05:25:23 · 197 阅读 · 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 · 204 阅读 · 0 评论 -
Evaluate Division
Equations are given in the formatA / B = k, whereAandBare variables represented as strings, andkis a real number (floating point number). Given some queries, return the answers. If the answer does not exist, return-1.0.Example:Givena / b = 2.0,...原创 2020-06-07 06:58:10 · 162 阅读 · 0 评论 -
Find a Corresponding Node of a Binary Tree in a Clone of That Tree
Given two binary treesoriginalandclonedand given a reference to a nodetargetin the original tree.Theclonedtree is acopy oftheoriginaltree.Returna reference to the same nodein theclonedtree.Notethat you arenot allowedto change any o...原创 2020-06-01 06:20:10 · 316 阅读 · 0 评论 -
Knight Dialer
A chess knight can move as indicated in the chess diagram below:. This time, we place our chess knight on any numbered key of a phone pad (indicated above), and the knight makesN-1hops. Each hop must be from one key to another numbered...原创 2020-05-31 05:34:46 · 174 阅读 · 0 评论 -
Strobogrammatic Number III
A strobogrammatic number is a number that looks the same when rotated 180 degrees (looked at upside down).Write a function to count the total strobogrammatic numbers that exist in the range of low <= num <= high.Example:Input: low = "50", high原创 2020-05-26 12:54:22 · 258 阅读 · 0 评论 -
Strobogrammatic Number II
A strobogrammatic number is a number that looks the same when rotated 180 degrees (looked at upside down).Find all strobogrammatic numbers that are of length = n.Example:Input: n = 2Output: ["11","69","88","96"]思路:这题就是用dfs来计算里面的,然后回归到最后一层,用里面的结原创 2020-05-26 12:31:12 · 222 阅读 · 0 评论 -
Longest Increasing Path in a Matrix
Given an integer matrix, find the length of the longest increasing path.From each cell, you can either move to four directions: left, right, up or down. You may NOT move diagonally or move outside o原创 2016-10-10 05:55:29 · 433 阅读 · 0 评论 -
Insert into a Binary Search Tree
Given the root node of a binary search tree (BST) and a value to be inserted into the tree,insert the value into the BST. Return the root node of the BST after the insertion. It is guaranteed that the new value does not exist in the original BST.Note th.原创 2020-05-22 07:20:18 · 179 阅读 · 0 评论 -
Partition Equal Subset Sum
Given anon-emptyarray containingonly positive integers, find if the array can be partitioned into two subsets such that the sum of elements in both subsets is equal.Note:Each of the array element will not exceed 100. The array size will not exceed ...原创 2020-05-20 14:13:57 · 229 阅读 · 0 评论 -
Maximum Vacation Days
LeetCode wants to give one of its best employees the option to travel amongNcities to collect algorithm problems. But all work and no play makes Jack a dull boy, you could take vacations in some particular cities and weeks. Your job is to schedule the tr..原创 2020-05-17 06:22:35 · 228 阅读 · 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 · 206 阅读 · 0 评论 -
Tree Diameter
Given an undirected tree, returnits diameter: the number ofedgesin a longest path in that tree.The tree is given as an arrayofedgeswhereedges[i] = [u, v]is a bidirectional edge between nodesuandv. Each node haslabels in the set{0, 1, ..., e...原创 2020-05-12 06:48:23 · 411 阅读 · 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 · 172 阅读 · 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 · 197 阅读 · 0 评论 -
Target Sum
You are given a list of non-negative integers, a1, a2, ..., an, and a target, S. Now you have 2 symbols+and-. For each integer, you should choose one from+and-as its new symbol.Find out how m...原创 2020-04-18 07:26:55 · 233 阅读 · 0 评论 -
Number of Distinct Islands
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-04-11 05:49:24 · 246 阅读 · 0 评论 -
Number of Closed Islands
Given a 2Dgridconsists of0s(land)and1s(water). Anislandis a maximal 4-directionally connected group of0sand aclosed islandis an islandtotally(all left, top, right, bottom) surrounded b...原创 2020-04-11 05:06:11 · 220 阅读 · 0 评论 -
Concatenated Words
Given a list of words (without duplicates), please write a program that returns all concatenated words in the given list of words.A concatenated word is defined as a string that is comprised entirel...原创 2020-04-11 02:56:10 · 252 阅读 · 0 评论