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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
// Author: Huahua
// Running time: 12 ms
classSolution{
public:
boolisBipartite(vector<vector<int>>& graph) {
const int n = graph.size();
vector<int>colors(n);
for(inti=0;i<n;++i)
if(!colors[i]&& !coloring(graph, colors, 1, i))
return false;
returntrue;
}
private:
boolcoloring(constvector<vector<int>>& graph, vector<int>& colors, int color, int node) {
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 .
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)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
// Author: Huahua
// Running time: 4 ms (<99.21%)
classSolution{
public:
boolcanPartitionKSubsets(vector<int>& nums, int k) {
const int sum = accumulate(nums.begin(), nums.end(), 0);
if(sum%k!=0)returnfalse;
sort(nums.rbegin(),nums.rend());
returndfs(nums,sum/k,0,k,0);
}
private:
booldfs(constvector<int>& nums, int target, int cur, int k, int used) {
if (k == 0) return used == (1 << nums.size()) - 1;
for(inti=0;i<nums.size();++i){
if(used& (1 << i)) continue;
intt=cur+nums[i];
if(t>target)break;// Important
intnew_used=used|(1<<i);
if(t==target&& dfs(nums, target, 0, k - 1, new_used)) return true;
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 = 2Output: [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:
The given tree is non-empty.
Each node in the tree has unique valuesĀ 0 <= node.val <= 500.
TheĀ targetĀ node is a node in the tree.
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.