All Questions
Tagged with depth-first-search programming-challenge
28 questions
10
votes
2
answers
4k
views
Generating the powerset in C
I am solving another problem in leetcode.
Given a set, generate all the subsets of the set.
Input: [1,2,3]
Output: ...
9
votes
1
answer
4k
views
Hacker Rank: Journey to the Moon (Graph Theory)
I am attempting to complete the "Journey to the Moon" problem described below:
There are N trained astronauts numbered from 0 to N-1. But those in
charge of the mission did not receive ...
8
votes
2
answers
3k
views
Make Depth First Search program more time efficient
This is a problem from programming contest. The original problem can be found here on hackerearth.com.
In the first line there is one integer T denoting the number of test
cases. Description of T ...
8
votes
2
answers
3k
views
Equivalent binary trees (A Tour of Go)
Any suggestions on how to improve the code shown below for the Go-tour exercise?
Exercise description:
There can be many different binary trees with the same sequence of
values stored at the ...
7
votes
1
answer
1k
views
Leetcode 17. Letter Combinations of a Phone Number
Problem Statement
Given a digit string, return all possible letter combinations that the number could represent.
A mapping of digit to letters (just like on the telephone buttons) is given below.
...
7
votes
1
answer
793
views
Cheapest flights with at most k stops
This is the Cheapest Flights Within K Stops problem from leetcode.com:
There are \$n\$ cities connected by \$m\$ flights. Each fight starts from city \$u\$ and arrives at \$v\$ with a price \$w\$.
...
6
votes
1
answer
1k
views
Topological sort using recursive DFS
I am currently learning Graph algorithms by solving questions on online judges. The below code is for implementing Topological sort, using recursive DFS. Also, it is my first time with C++ STL. ...
5
votes
2
answers
1k
views
(Leetcode) Minesweeper game
This is a Leetcode problem -
Let's play the Minesweeper game
(Wikipedia,
online game)!
You are given a 2D char matrix representing the game board. 'M'
represents an unrevealed mine, 'E' ...
5
votes
1
answer
227
views
LeetCode: Add and Search Word C#
This is a Leetcode problem:
Design a data structure that supports the following two operations:
void addWord(word)
...
4
votes
2
answers
1k
views
Given two words and a dictionary's word list, find all shortest transformation sequence
This is a leetcode problem (https://leetcode.com/problems/word-ladder-ii/description/)
Given two words (beginWord and endWord), and a dictionary's word list, find all shortest transformation sequence(...
4
votes
1
answer
352
views
LeetCode 839: Similar String Groups II
I'm posting my code for a LeetCode problem. If you'd like to review, please do so. Thank you for your time!
Problem
Two strings X and Y are similar if we can swap two letters (in different positions) ...
4
votes
1
answer
2k
views
(Leetcode) 24 game in Python
This is a Leetcode problem -
You have 4 cards each containing a number from 1 to 9. You need to
judge whether they could be ...
4
votes
1
answer
2k
views
Find all concatenated words in a given list of words
This is a Leetcode problem -
Given a list of words (without duplicates), write a program that
returns all concatenated words in the given list of words.
A concatenated word is defined as a ...
4
votes
1
answer
177
views
Two versions of Word Break II - exhaustive combinations
I am solving Word Break II from leetcode:
Given a non-empty string s and a dictionary wordDict containing a list of non-empty ...
4
votes
2
answers
251
views
Finding a vertex of a tree such that every subtree rooted at that vertex is of a uniform color
I'm trying to optimally solve codeforces problem 763A:
Each New Year Timofey and his friends cut down a tree of n vertices
and bring it home. After that they paint all the n its vertices, so
...
3
votes
2
answers
1k
views
Find all possible words in a board of characters
This question has 2 possible solutions and I am trying to implement the DFS solution.
Here is the Trie and DFS solution: Boggle using Trie and DFS
Please review for performance.
Given a ...
3
votes
1
answer
2k
views
"The Story of a Tree" solved using depth-first search
Found this problem in hackerrank.
One day Bob drew a tree, with \$n\$ nodes and \$n-1\$ edges on a piece of paper. He soon discovered that parent of a node depends on the root of the tree. The ...
3
votes
1
answer
840
views
Ternary tree preoder traversal (recursive implementation)
Problem statement:
Print out to console the ternary tree preorder traversal. Suppose that the tree can have at most 3 children, left, middle and right. Preorder traversal is to visit the middle ...
3
votes
1
answer
2k
views
Maze path finder using Depth-First Search algorithm
I'm trying to resolve this kata from CodeWars.
Kata exercise
You are at position [0, 0] in maze NxN and you can only move in one of the four cardinal directions (i.e. North, East, South, West). ...
3
votes
1
answer
305
views
Find number of islands Leetcode
Here is the question:
Given a 2d grid map of '1's (land) and '0's (water), count the number
of islands. An island is surrounded by water and is formed by
connecting adjacent lands horizontally ...
2
votes
2
answers
263
views
Leetcode: Flatten binary tree to linked list C#
https://leetcode.com/problems/flatten-binary-tree-to-linked-list/
Please comment on performance
Given a binary tree, flatten it to a linked list in-place.
For example, given the following ...
1
vote
1
answer
221
views
LeetCode: FloodFill C#
https://leetcode.com/problems/flood-fill/
An image is 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,...
1
vote
1
answer
269
views
(Leetcode) Freedom Trail
I would like to know whether I could make my solution to this challenge shorter and more efficient. Below is the description of the challenge (this is a Leetcode problem) -
In the video game ...
1
vote
0
answers
368
views
Codewars: Path Finder
Task
You are at position [0, 0] in maze NxN and you can only move in one of the four cardinal directions (i.e. North, East, South, West). Return true if you can reach position [N-1, N-1] or false ...
1
vote
0
answers
302
views
LeetCode:MineSweeper C#
https://leetcode.com/problems/minesweeper/
please review for performance. I really don't like the implementation I made for GetNumberOfMines() function, but within the time limits i did this. it is ...
1
vote
0
answers
304
views
Graph-coloring solution using depth-first search
I'm trying to solve this question:
Andryusha goes through a park each day. The squares and paths between them look boring to Andryusha, so he decided to decorate them.
The park consists of n squares ...
0
votes
1
answer
250
views
Project Euler#18: Maximum Paths I: Brute Forced Depth First Search
Problem:
Data:
...
0
votes
1
answer
354
views
At least 2 paths down the binary tree have the same sum
Problem statement
Write a function that given a binary tree, returns true if at least 2 paths down the tree have the same sum. A path is a series of nodes from the root to the leaf.
Examples:
...