LeetCode 886. Possible Bipartition
原题链接在这里:https://leetcode.com/problems/possible-bipartition/
题目:
Given a set of N
people (numbered 1, 2, ..., 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.
Formally, if dislikes[i] = [a, b]
, it means it is not allowed to put the people numbered a
and b
into the same group.
Return true
if and only if it is possible to split everyone into two groups in this way.
Example 1:
- Input: N = 4, dislikes = [[1,2],[1,3],[2,4]]
- Output: true
- Explanation: group1 [1,4], group2 [2,3]
Example 2:
- Input: N = 3, dislikes = [[1,2],[1,3],[2,3]]
- Output: false
Example 3:
- Input: N = 5, dislikes = [[1,2],[2,3],[3,4],[4,5],[1,5]]
- Output: false
Note:
1 <= N <= 2000
0 <= dislikes.length <= 10000
1 <= dislikes[i][j] <= N
dislikes[i][0] < dislikes[i][1]
- There does not exist
i != j
for whichdislikes[i] == dislikes[j]
.
题解:
To determine it is bipartition, we could see if we could color them into 2 different colors.
First use dislikes to construct a graph.
For the node hasn't been color before and it is in the graph, put it as one color 1, then perform BFS.
For cur polled number, if its neighbor has the same color, then return false.
Time Complexity: O(N+E). E = dislikes.length.
Space: O(N).
AC Java:
- class Solution {
- public boolean possibleBipartition(int N, int[][] dislikes) {
- Map<Integer, Set<Integer>> graph = new HashMap<>();
- for(int [] d : dislikes){
- graph.putIfAbsent(d[0], new HashSet<Integer>());
- graph.putIfAbsent(d[1], new HashSet<Integer>());
- graph.get(d[0]).add(d[1]);
- graph.get(d[1]).add(d[0]);
- }
- int [] color = new int[N+1];
- for(int i = 1; i<=N; i++){
- if(color[i]==0 && graph.containsKey(i)){
- color[i] = 1;
- LinkedList<Integer> que = new LinkedList<>();
- que.add(i);
- while(!que.isEmpty()){
- int cur = que.poll();
- for(int nei : graph.get(cur)){
- if(color[nei] == 0){
- color[nei] = -color[cur];
- que.add(nei);
- }else if(color[nei] == color[cur]){
- return false;
- }
- }
- }
- }
- }
- return true;
- }
- }
LeetCode 886. Possible Bipartition的更多相关文章
- 【LeetCode】886. Possible Bipartition 解题报告(Python)
[LeetCode]886. Possible Bipartition 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu ...
- leetcode 886. 可能的二分法(DFS,染色,种类并查集)
题目链接 886. 可能的二分法 题意: 给定一组 N 人(编号为 1, 2, ..., N), 我们想把每个人分进任意大小的两组. 每个人都可能不喜欢其他人,那么他们不应该属于同一组. 形式上,如果 ...
- leetcode 890. Possible Bipartition
Given a set of N people (numbered 1, 2, ..., N), we would like to split everyone into two groups of ...
- All LeetCode Questions List 题目汇总
All LeetCode Questions List(Part of Answers, still updating) 题目汇总及部分答案(持续更新中) Leetcode problems clas ...
- 算法与数据结构基础 - 深度优先搜索(DFS)
DFS基础 深度优先搜索(Depth First Search)是一种搜索思路,相比广度优先搜索(BFS),DFS对每一个分枝路径深入到不能再深入为止,其应用于树/图的遍历.嵌套关系处理.回溯等,可以 ...
- [LeetCode] Possible Bipartition 可能的二分图
Given a set of N people (numbered 1, 2, ..., N), we would like to split everyone into two groups of ...
- Leetcode(886)-可能的二分法
给定一组 N 人(编号为 1, 2, ..., N), 我们想把每个人分进任意大小的两组. 每个人都可能不喜欢其他人,那么他们不应该属于同一组. 形式上,如果 dislikes[i] = [a, b] ...
- Swift LeetCode 目录 | Catalog
请点击页面左上角 -> Fork me on Github 或直接访问本项目Github地址:LeetCode Solution by Swift 说明:题目中含有$符号则为付费题目. 如 ...
- [LeetCode] Is Graph Bipartite? 是二分图么?
Given an undirected graph, return true if and only if it is bipartite. Recall that a graph is bipart ...
随机推荐
- AtCoder-arc059 (题解)
A - いっしょ / Be Together (结论/暴力) 题目链接 题目大意: 有 \(n\) 个数字,要将它们变成相等,对每一个数字最多操作一次,如将 \(a \to b\) 的代价为 \((a ...
- Linux平台上常用到的c语言开发程序
Linux操作系统上大部分应用程序都是基于C语言开发的.小编将简单介绍Linux平台上常用的C语言开发程序. 一.C程序的结构1.函数 必须有一个且只能有一个主函数main(),主函数的名为main. ...
- Visual Studio pro key license 2019
仅供学习交流使用,勿用作其他用途!!!! Visual Studio 2019 Enterprise BF8Y8-GN2QH-T84XB-QVY3B-RC4DF Visual Studio 201 ...
- Elasticsearch 、 Logstash以及Kibana 分布式日志
搭建ELK日志分析平台(上)—— ELK介绍及搭建 Elasticsearch 分布式集群 ELK简介: ELK是三个开源软件的缩写,分别为:Elasticsearch . Logstash以及Kib ...
- VsCode中编写python环境配置
1. VsCode中编写python环境配置 1.1. 前言 有过开发经验都知道idea一系列的软件虽然功能比较多,但比较容易卡,电脑不好还真容易上火,这里我想要入门python,还是选了款vscod ...
- HTTP协议中GET和POST
1. get 它用于获取信息,注意,他只是获取.查询数据,也就是说它不会修改服务器上的数据,从这点来讲,它是数据安全的,而稍后会提到的Post它是可以修改数据的,所以这也是两者差别之一了 2. pos ...
- Echarts实现Excel趋势线和R平方计算思路
测试数据 [19550, 7.1 ],[22498, 8.44 ],[25675, 9.56 ],[27701, 10.77],[29747, 11.5 ],[32800, 12.27],[34822 ...
- ML-线性模型 泛化优化 之 L1 L2 正则化
认识 L1, L2 从效果上来看, 正则化通过, 对ML的算法的任意修改, 达到减少泛化错误, 但不减少训练误差的方式的统称 训练误差 这个就损失函数什么的, 很好理解. 泛化错误 假设 我们知道 预 ...
- Shell 编程 循环语句
本篇主要写一些shell脚本循环语句的使用. for 循环 指定次数 #!/bin/bash for ((i=1;i<=10;i++)) do echo $i done [root@localh ...
- google批量搜索实现方式
本文主要记录一下最近所做的关于Google批量搜索的实现方式. 搜索目的: 获取关键词在某个域名下对应的Google搜索结果数 搜索方式: 关键词+inurl 例如:"爬虫" in ...