Press "Enter" to skip to content

Posts tagged as “DFS”

花花酱 LeetCode 785. Is Graph Bipartite?

Video is forĀ čŠ±čŠ±é…± LeetCode 886. Possible Bipartition, but the algorithm is exact the same.

Problem

https://leetcode.com/problems/is-graph-bipartite/

Given an undirectedĀ graph, returnĀ trueĀ if and only if it is bipartite.

Recall that a graph isĀ bipartiteĀ if we can split it’s set of nodes into two independentĀ subsets A and B such that every edge in the graph has one node in A and another node in B.

The graph is given in the following form:Ā graph[i]Ā is a list of indexesĀ jĀ for which the edge between nodesĀ iĀ andĀ jĀ exists.Ā  Each node is an integer betweenĀ 0Ā andĀ graph.length - 1.Ā  There are no self edges or parallel edges:Ā graph[i]Ā does not containĀ i, and it doesn’t contain any element twice.

Example 1:
Input: [[1,3], [0,2], [1,3], [0,2]]
Output: true
Explanation: 
The graph looks like this:
0----1
|    |
|    |
3----2
We can divide the vertices into two groups: {0, 2} and {1, 3}.
Example 2:
Input: [[1,2,3], [0,2], [0,1,3], [0,2]]
Output: false
Explanation: 
The graph looks like this:
0----1
| \  |
|  \ |
3----2
We cannot find a way to divide the set of nodes into two independent subsets.

 

Note:

  • graphĀ will have length in rangeĀ [1, 100].
  • graph[i]Ā will contain integers in rangeĀ [0, graph.length - 1].
  • graph[i]Ā will not containĀ iĀ or duplicate values.
  • The graph is undirected: if any elementĀ jĀ is inĀ graph[i], thenĀ iĀ will be inĀ graph[j].

Solution: Graph Coloring

For each node

  • If has not been colored, color it to RED(1).
  • Color its neighbors with a different color RED(1) to BLUE(-1) or BLUE(-1) to RED(-1).

If we can finish the coloring then the graph is bipartite. All red nodes on the left no connections between them and all blues nodes on the right, again no connections between them. red and blue nodes are neighbors.

Time complexity: O(V+E)

Space complexity: O(V)

C++ / DFS

Related Problem

花花酱 LeetCode 491. Increasing Subsequences

Problem

Given an integer array, your task is to find all the different possible increasing subsequences of the given array, and the length of an increasing subsequence should be at least 2 .

Example:

Input: [4, 6, 7, 7]
Output: [[4, 6], [4, 7], [4, 6, 7], [4, 6, 7, 7], [6, 7], [6, 7, 7], [7,7], [4,7,7]]

Note:

  1. The length of the given array will not exceed 15.
  2. The range of integer in the given array is [-100,100].
  3. The given array may contain duplicates, and two equal integers should also be considered as a special case of increasing sequence.

Solution: DFS

Time complexity: O(2^n)

Space complexity: O(n)

C++

 

花花酱 LeetCode 698. Partition to K Equal Sum Subsets

Problem

Given an array of integersĀ numsĀ and a positive integerĀ k, find whether it’s possible to divide this array intoĀ knon-empty subsets whose sums are all equal.

Example 1:

Input: nums = [4, 3, 2, 3, 5, 2, 1], k = 4
Output: True
Explanation: It's possible to divide it into 4 subsets (5), (1, 4), (2,3), (2,3) with equal sums.

Note:

  • 1 <= k <= len(nums) <= 16.
  • 0 < nums[i] < 10000.

Solution: Search

Time complexity: O(n!)

Space complexity: O(n)

 

花花酱 LeetCode 863. All Nodes Distance K in Binary Tree

Problem

题ē›®å¤§ę„ļ¼šē»™ä½ äø€ę£µäŗŒå‰ę ‘ļ¼ˆę ¹ē»“ē‚¹rootļ¼‰å’Œäø€äøŖtarget节ē‚¹ć€‚čæ”å›žę‰€ęœ‰åˆ°targetēš„č·ē¦»äøŗKēš„节ē‚¹ć€‚

We are given a binary tree (with root nodeĀ root), aĀ targetĀ node, and an integer value K.

Return a list of the values of allĀ nodes that have a distanceĀ KĀ from theĀ targetĀ node.Ā  The answer can be returned in any order.

Example 1:

Input: root = [3,5,1,6,2,0,8,null,null,7,4], target = 5, K = 2
Output: [7,4,1]
Explanation: 
The nodes that are a distance 2 from the target node (with value 5)
have values 7, 4, and 1.

Note that the inputs "root" and "target" are actually TreeNodes.
The descriptions of the inputs above are just serializations of these objects.

Note:

  1. The given tree is non-empty.
  2. Each node in the tree has unique valuesĀ 0 <= node.val <= 500.
  3. TheĀ targetĀ node is a node in the tree.
  4. 0 <= K <= 1000.

Solution1: DFS + BFS

Use DFS to build the graph, and use BFS to find all the nodes that are exact K steps from target.

Time complexity: O(n)

Space complexity: O(n)

C++

Array version

Solution 2: Recursion

Recursively compute the distance from root to target, and collect nodes accordingly.

Time complexity: O(n)

Space complexity: O(n)

 

花花酱 LeetCode 841. Keys and Rooms

Problem

There areĀ NĀ rooms and you start in roomĀ 0.Ā  Each room has a distinct number inĀ 0, 1, 2, ..., N-1, and each room may haveĀ some keys to access the next room.

Formally, each roomĀ iĀ has a list of keysĀ rooms[i], and each keyĀ rooms[i][j]Ā is an integer inĀ [0, 1, ..., N-1]Ā whereĀ N = rooms.length.Ā  A keyĀ rooms[i][j] = vĀ opens the room with numberĀ v.

Initially, all the rooms start locked (except for roomĀ 0).

You can walk back and forth between rooms freely.

ReturnĀ trueĀ if and only if you can enterĀ every room.

Example 1:

Input: [[1],[2],[3],[]]
Output: true
Explanation:  
We start in room 0, and pick up key 1.
We then go to room 1, and pick up key 2.
We then go to room 2, and pick up key 3.
We then go to room 3.  Since we were able to go to every room, we return true.

Example 2:

Input: [[1,3],[3,0,1],[2],[0]]
Output: false
Explanation: We can't enter the room with number 2.

Note:

  1. 1 <= rooms.length <=Ā 1000
  2. 0 <= rooms[i].length <= 1000
  3. The number of keys in all rooms combined is at mostĀ 3000.

Solution: DFS

Time complexity: O(V + E)

Space complexity: O(V)

C++