Given a set of n people (numbered from 1 to n), we would like to split everyone into two groups of any size. Each person may dislike some other people, and they should not go into the same group.
We are given a 2D array dislikes where each dislikes[i] = [a_i, b_i] indicates that person a_i dislikes person b_i. Return true if and only if it is possible to split everyone into two groups in this way.
n?
1 ≤ n ≤ 2000.dislikes array?
dislikes array will not contain self-loops? (i.e., person dislikes themselves)
To solve this problem, we can model it as a graph problem:
false.true.from collections import defaultdict, deque
def possibleBipartition(n, dislikes):
# Step 1: Create an adjacency list for the graph
graph = defaultdict(list)
for u, v in dislikes:
graph[u].append(v)
graph[v].append(u)
# Step 2: Initialize a color array for nodes (0: uncolored, 1: color1, -1: color2)
color = [0] * (n + 1)
# Step 3: BFS/DFS to check for bipartiteness
def bfs(start):
queue = deque([start])
color[start] = 1 # Start coloring with color 1
while queue:
node = queue.popleft()
for neighbor in graph[node]:
if color[neighbor] == 0: # If the neighbor hasn't been colored yet
color[neighbor] = -color[node] # Color with the opposite color
queue.append(neighbor)
elif color[neighbor] == color[node]: # If the neighbor has the same color
return False
return True
# Step 4: Check each component of the graph
for person in range(1, n + 1):
if color[person] == 0: # Not yet colored, thus needs to be checked
if not bfs(person):
return False
return True
dislikes array.1 to n) and E is the number of edges (dislikes).Thus, the overall time complexity is O(V + E), which is efficient for the given constraints.
Got blindsided by a question you didn’t expect?
Spend too much time studying?
Or simply don’t have the time to go over all 3000 questions?