← Home

886. Possible Bipartition

LeetCode article · C++ solution
Website made by wuisabel-gif · Original C++ code by keineahnung2345
graph traversalC++Markdown
886

This is one of those problems where the clean idea matters more than the amount of code. For 886. Possible Bipartition, the solution in this repository is mainly a graph traversal solution.

Guide

What?

The first job is to translate the English prompt into state, transition, and stopping conditions. Instead of trying to be clever immediately, read the code as a sequence of questions:

  • What state are we keeping?
  • How do we move from one state to the next?
  • When do we know the answer is already determined?

For this file, the main tools are: graph traversal.

The notes already sitting in the source point us in the right direction:

  • graph
  • Approach 1: Depth-First Search
  • time: O(N+E), space: O(N+E)

Guide

When?

This is the kind of solution you want when the problem has structure hiding inside a messy-looking input. The accepted code reduces that pressure by storing exactly the information that remains useful later.

The important function names to track are dfs, possibleBipartition.

Guide

Why?

The point of the implementation is not to make the code longer. It is to avoid doing the same thinking twice.

  • A map keeps the lookup side cheap; the code pays a little memory to avoid repeated searching.
  • The two-dimensional vector is the memory of the solution: grid state, DP state, or adjacency shape.
  • The final return is not magic; it is the invariant after the loops or recursion have finished doing their accounting.

Guide

How?

Walk through the solution in this order:

  1. Start from the smallest reliable state.
  2. Expand one legal move at a time.
  3. Cache, count, or merge information as soon as it becomes settled.
  4. Let the final stored value answer the original question.

The most important competitive-programming habit here is to trust the invariant. Once the invariant is right, the loops become much less scary.

Guide

Complexity

  • Time: O(N+E), space: O(N+E)
  • Space: O(n) in the usual case for auxiliary containers or recursion

Guide

C++ Solution

Your submission

The accepted solution

solution.cpp
01//graph
02//Approach 1: Depth-First Search
03//time: O(N+E), space: O(N+E)
04//Runtime: 488 ms, faster than 29.22% of C++ online submissions for Possible Bipartition.
05//Memory Usage: 66.5 MB, less than 14.29% of C++ online submissions for Possible Bipartition.
06class Solution {
07public:
08    vector<vector<int>> graph;
09    map<int, int> color;
10    
11    bool dfs(int node, int c){
12        if(color.find(node) != color.end()){
13            //a conflict occurs if the color assigned is different from the color we want to assign
14            return color[node] == c;
15        }
16        
17        color[node] = c;
18        
19        for(int nei : graph[node]){
20            if(!dfs(nei, 1-c)){
21                return false;
22            }
23        }
24        
25        return true;
26    }
27    
28    bool possibleBipartition(int N, vector<vector<int>>& dislikes) {
29        //padding before
30        graph = vector<vector<int>>(N+1);
31        
32        for(vector<int>& edge : dislikes){
33            graph[edge[0]].push_back(edge[1]);
34            graph[edge[1]].push_back(edge[0]);
35        }
36        
37        for(int node = 1; node <= N; ++node){
38            //skip visited node
39            if(color.find(node) == color.end() && 
40              !dfs(node, 0) //try to assign the node to group 0 and see if it success
41              ){
42                return false;
43            }
44        }
45        
46        return true;
47    }
48};

Cost

Complexity

Time
O(N+E), space: O(N+E)
Dominated by the main traversal, recursion, or data-structure operations in the code.
Space
O(n) in the usual case for auxiliary containers or recursion
Auxiliary state plus the answer structure where the problem requires one.